-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Legacy Complex Primary keys
PostPosted: Sat Apr 12, 2008 4:35 am 
Newbie

Joined: Thu Apr 10, 2008 12:09 pm
Posts: 4
I have a problem with a legacy oracle database structure where I am not allowed to change the schema. My EE container is OC4J.


I have three interconnected tables.

Product
ID - Primary Key

ProductEvent
ID - Not Unique but with PR_ID forms Primary Key
PR_ID - Foreign Key to Product

ProductData
ID - Not Unique but with PR_ID forms Primary Key
PR_ID - Foreign Key to Product
PREV_ID - With PR_ID forms foreign key to Event

I have three entities summarised as below. (These were mostly generated by jdeveloper)

Code:
@Entity
public class Product implements Serializable {
   @Id
   @Column(nullable = false)
   private String id;
   
   @OneToMany(mappedBy = "product", fetch = FetchType.EAGER, cascade={CascadeType.PERSIST})
   private List<ProductData> productDataList;
   
   @OneToMany(mappedBy = "product", fetch = FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE})
   private List<ProductEvent> productEventList;
}


@Entity
@IdClass(ProductEventPK.class)
public class ProductEvent implements Serializable {
   @Temporal(TemporalType.DATE) 
   @Column(name="DATE_TIME", nullable = false)
   private Date dateTime;

   @Id
   @Column(nullable = false)
   private String id;

   @Id
   @Column(name="PR_ID", nullable = false, insertable = false, updatable = false)
   private String prId;

   @ManyToOne
   @JoinColumn(name = "PR_ID", referencedColumnName = "ID")
   private Product product;

   @OneToOne(mappedBy = "event")
   private ProductData productData;
}

@Entity
@IdClass(ProductDataPK.class)
public class ProductData implements Serializable{
   @Id
   @Column(nullable = false)
   private String id;

   @Id
   @Column(name="PR_ID", nullable = false)
   private String prId; 

   @Column(name="PREV_ID", nullable = false)
   private String prevId;

   @OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
   @JoinColumns({
      @JoinColumn(name = "PR_ID", referencedColumnName = "PR_ID", nullable = false, insertable = false, updatable = false),
      @JoinColumn(name = "PREV_ID", referencedColumnName = "ID")
   })
   private ProductEvent event;

   @ManyToOne
   @JoinColumn(name = "PR_ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false)
   private Product product;
}
I've tried having the one to one bidirectional and unidirectional. The code as above works using EclipseLink but when using Hibernate I get an error "Unable to create EntityManagerFactory". This only happens when I try to add the one to one relationship.

Any pointers to what I've missed will be most appreciated.
(PS. when I get this working I also have 3 more tables to add which also have the same links as productdata to product and productevent. This is used to identify what was the data on date xxx)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 12, 2008 5:25 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I get an error "Unable to create EntityManagerFactory"

It could be easier to help you if we could see the stacktrace.

Sanne


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 12, 2008 6:13 am 
Newbie

Joined: Thu Apr 10, 2008 12:09 pm
Posts: 4
I agree, but the information I've got in front of me right now doesn't include the stack trace. I will be able to debug through later this morning. Somewhere in the inhouse framework it's throwing away the stack trace at the moment. Once I've found where I'll post the trace.

Thanks
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 12, 2008 12:15 pm 
Newbie

Joined: Thu Apr 10, 2008 12:09 pm
Posts: 4
Further to my previous messages. I've got an annotation exception inside my previously mentioned persistence exception. The text is ..

Code:
referencedColumnNames(PR_ID, ID) of ProductData.event referencing ProductEvent not mapped to a single property


and the stack trace is
Code:
08/04/12 17:11:29 Caused by: org.hibernate.AnnotationException: referencedColumnNames(PR_ID, ID) of ProductData.event referencing ProductEvent not mapped to a single property
08/04/12 17:11:29    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:180)
08/04/12 17:11:29    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:88)
08/04/12 17:11:29    at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:499)
08/04/12 17:11:29    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:304)
08/04/12 17:11:29    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
08/04/12 17:11:29    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1225)
08/04/12 17:11:29    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:159)
08/04/12 17:11:29    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
08/04/12 17:11:29    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
08/04/12 17:11:29    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 12, 2008 5:13 pm 
Newbie

Joined: Thu Apr 10, 2008 12:09 pm
Posts: 4
I got this fixed by changing the id to use an embeddedId and thus making it one property that then mapped happily. I then found a few other niggles in the annotations that I fixed.

Thanks
Richard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.