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 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> 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 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=20 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=20 ignored." http://docs.oracle.com/javaee/6/tutorial/doc/bnbqn.html#bnbqq > @Column(name =3D "ID") > @GeneratedValue(strategy =3D GenerationType.SEQUENCE, generator =3D= =20 > "IdSequenceGen") You don't show this class as an entity, so how does it generate an ID, or m= erit one? > @Id > public Long getId() > { > return this.id; > } > [...] > } >=20 > An entity Class, extending Class1, that has a composite PK: The 'Class1'= =20 > 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 =3D InheritanceType.TABLE_PER_CLASS) > @SequenceGenerator(name =3D "IdSequenceGen", sequenceName =3D "SQ_CLASS2"= ) Does it use a sequence ID or does it use a composite ID, which? > @Table(name =3D "CIM_PERSON_DEMOGRAPHIC") > public class Class2 extends Class1{ Why exactly are you inheriting from 'Class1'? > private Integer prog =3D 0; Why are you initializing 'prog' redundantly? You should provide a comment w= hen=20 you do something strange. > @Column(name =3D "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; > } > [...] > } >=20 > But in this case I've this error at runtime: > javax.persistence.PersistenceException: [PersistenceUnit: datamodel]=20 > Unable to configure EntityManagerFactory > at=20 > org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:375) > at=20 > org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(Hiberna= tePersistence.java:56) > at=20 > javax.persistence.Persistence.createEntityManagerFactory(Persistence.java= :48) > at=20 > javax.persistence.Persistence.createEntityManagerFactory(Persistence.java= :32) > at=20 > com.noemalife.platform.dm.test.AbstractTest.buildEntityManager(AbstractTe= st.java:111) > Caused by: org.hibernate.AnnotationException: Unknown Id.generator:=20 > IdSequenceGen >=20 > How can i [sic] solve this problem? Use the annotations as documented, pick ONE primary key for 'Class2' (or=20 whatever good name you actually give it), have a cleaner data model, map it= =20 simply, don't get so fancy. Show us an SSCCE with data model. On the face of it you are trying to shoeh= orn a wacky data model (that you haven't shared with us) onto a twisted dat= a model (that you haven't shared with us). That's a recipe for disaster. In= stead, define a straightforward entity model, a separate straightforward re= lational 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.=20 They don't have silly integer keys, either. And I note that you haven't=20 provided 'equals()' and 'hashCode()' overrides either (which, of course, me= ans=20 that you haven't overridden 'toString()' either, have you?). And if any=20 entities implement 'Comparable' that's another method that has to be consis= tent=20 with those three. Have you read the Java EE tutorial (to which I linked above) about JPA? --=20 Lew