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)