Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1556
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: JPA and composite primary key |
| Date | 2012-02-07 10:45 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <21458011.111.1328640309137.JavaMail.geo-discussion-forums@pbcpg7> (permalink) |
| References | <4f30ed2a$1@news.x-privat.org> |
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 | Next — Previous in thread | Find similar | Unroll 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