Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1556

Re: JPA and composite primary key

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!198.186.194.250.MISMATCH!news-out.readnews.com!news-xxxfer.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: JPA and composite primary key
Date Tue, 7 Feb 2012 10:45:09 -0800 (PST)
Organization http://groups.google.com
Lines 123
Message-ID <21458011.111.1328640309137.JavaMail.geo-discussion-forums@pbcpg7> (permalink)
References <4f30ed2a$1@news.x-privat.org>
Reply-To comp.lang.java.help@googlegroups.com
NNTP-Posting-Host 173.164.137.214
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1328640309 21646 127.0.0.1 (7 Feb 2012 18:45:09 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Tue, 7 Feb 2012 18:45:09 +0000 (UTC)
In-Reply-To <4f30ed2a$1@news.x-privat.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:1556

Show key headers only | View raw


Short wrote:
> Here's the scenario:

This is not very much to go on, but I'll do the best I can. Can you work up a 
Simple, Self-Contained Compilable Example (SSCCE: http:/sscce.org/) please?

> An abstract class that defines an Id "generated":
> public abstract Class1{
>      [...]

What about the '@Entity' or '@MappedSuperClass' annotation?

"Any mapping or relationship annotations in non-entity superclasses are 
ignored."
http://docs.oracle.com/javaee/6/tutorial/doc/bnbqn.html#bnbqq

>      @Column(name = "ID")
>      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = 
> "IdSequenceGen")

You don't show this class as an entity, so how does it generate an ID, or merit one?

>      @Id
>      public Long getId()
>      {
>          return this.id;
>      }
>      [...]
> }
> 
> An entity Class, extending Class1, that has a composite PK: The 'Class1' 
> ID and a second field:

If it extends 'Class1' (terrible name), then it needs a discriminator, not a composite key.

> @Entity
> @IdClass(Class2.class)

It's its own ID class? That can't be right.

> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
> @SequenceGenerator(name = "IdSequenceGen", sequenceName = "SQ_CLASS2")

Does it use a sequence ID or does it use a composite ID, which?

> @Table(name = "CIM_PERSON_DEMOGRAPHIC")
> public class Class2 extends Class1{

Why exactly are you inheriting from 'Class1'?

>      private Integer prog = 0;

Why are you initializing 'prog' redundantly? You should provide a comment when 
you do something strange.

>      @Column(name = "PROG")
>      @Id

That can't be right. You said you were trying to create a composite ID, but here you are defining a single-column surrogate-key ID.

>      public Integer getProg()
>      {
>          return prog;
>      }
>      [...]
> }
> 
> But in this case I've this error at runtime:
> javax.persistence.PersistenceException: [PersistenceUnit: datamodel] 
> Unable to configure EntityManagerFactory
> 	at 
> org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:375)
> 	at 
> org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
> 	at 
> javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
> 	at 
> javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
> 	at 
> com.noemalife.platform.dm.test.AbstractTest.buildEntityManager(AbstractTest.java:111)
> Caused by: org.hibernate.AnnotationException: Unknown Id.generator: 
> IdSequenceGen
> 
> How can i [sic] solve this problem?

Use the annotations as documented, pick ONE primary key for 'Class2' (or 
whatever good name you actually give it), have a cleaner data model, map it 
simply, don't get so fancy.

Show us an SSCCE with data model. On the face of it you are trying to shoehorn a wacky data model (that you haven't shared with us) onto a twisted data model (that you haven't shared with us). That's a recipe for disaster. Instead, define a straightforward entity model, a separate straightforward relational data model, then figure out the mapping. Mapping of relationships is done through entities in JPA, not columns - defining relationships with columns in JPA is a mistake that tyros always make.

Don't think of objects in terms of columns - objects don't have columns. 
They don't have silly integer keys, either. And I note that you haven't 
provided 'equals()' and 'hashCode()' overrides either (which, of course, means 
that you haven't overridden 'toString()' either, have you?). And if any 
entities implement 'Comparable' that's another method that has to be consistent 
with those three.

Have you read the Java EE tutorial (to which I linked above) about JPA?

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

JPA and composite primary key Short <lamia@mail.com> - 2012-02-07 10:21 +0100
  Re: JPA and composite primary key Lew <lewbloch@gmail.com> - 2012-02-07 10:45 -0800

csiph-web