TableA has a 3 column composite primary key, TableB has a 2 column composite key primary key (these 2 columns are in TableA) and TableC has a single column primary key but has a composite foreign key from TableB.
This is an inherited table structure which I cannot change.
TableA 1:1 TableB
TableB M:1 TableA
TableB 1:M TableC
TableC M:1 TableB
UNB_NETWORK_CLINIC_PROVIDER -
TABLE ANETWORK_NO (PK)
CLINIC_NO (PK)
PROVIDER_NO (PK)
DELTA_BEGIN_DATE
DELTA_END_DATE
UNB_CLINIC_PROVIDER -
TABLE BCLINIC_NO (PK)
PROVIDER_NO (PK)
DELTA_BEGIN_DATE
DELTA_END_DATE
UNB_FOCUS_REVIEW -
TABLE CFOCUS_REVIEW_NO (PK)
PROVIDER_NO(FK)
CLINIC_NO(FK)
PROC_NO_FROM
PROC_NO_TO
I have used OneToMany JPA annotation to model the mapping between TableB and TableC using the composite key in the mappedBy attribute. Please see code below in namely UnbClinicProv.java and UnbFocusReview.java. This worked when the composite key of TableB was a foriegn key in TableC.
I tried (the same thing I thought) for the OntToOne mapping in UnbNetworkClinicProvider.java but this gives the error below.
Is it possible to model the OneToOne mapping between TableA and TableB using JPA annotations when the composite key of TableB is not a full foriegn key in TableA?
If yes how?
Please see below for the tables, code snippets and error I get;
Any help/comments appreciated.
Code:
Caused by: org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.dds.adjudication.provider.hibernate.UnbClinicProv.clinicProvKey in mappedBy of com.dds.adjudication.provider.hibernate.UnbNetworkClinicProvider.unbClinicProv
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:203)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1163)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:673)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 52 more
UnbClinicProv.java
Code:
//imports...
@Entity
@Table(name = "UNB_CLINIC_PROV")
public class UnbClinicProv implements Serializable,ClinicProv {
@EmbeddedId
private ClinicProvCompoundKey clinicProvKey;
@Column(name = "PROVIDER_NO",insertable=false,updatable=false)
private Long providerNo;
@Column(name = "CLINIC_NO",insertable=false,updatable=false)
private Long clinicNo;
@Column(name = "DELTA_BEGIN_DATE")
@org.hibernate.annotations.Type(type="com.dds.util.hibernate.DateMidnightType")
private DateMidnight deltaBeginDate;
@Column(name = "DELTA_END_DATE")
@org.hibernate.annotations.Type(type="com.dds.util.hibernate.DateMidnightType")
private DateMidnight deltaEndDate;
@OneToMany(mappedBy="frClinicProvKey",fetch = FetchType.LAZY)
@org.hibernate.annotations.OptimisticLock(excluded=true)
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL})
private List<UnbFocusReview> focusReviews;
//getters, setters, hashCode, equals, constructors....
}
UnbFocusReview.java
Code:
//imports...
@Entity
@Table(name = "UNB_FOCUS_REVIEW")
public class UnbFocusReview implements Serializable,FocusReviewInfo {
@Id
@SequenceGenerator(name="UNB_FOCUS_REVIEW_seq",sequenceName="UNB_FOCUS_REVIEW_seq",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="UNB_FOCUS_REVIEW_seq")
@Column(name = "FOCUS_REVIEW_NO")
private Long focusReviewNo;
private ClinicProvCompoundKey frClinicProvKey;
@Column(name = "PROC_NO_FROM")
private Long procNoFrom;
@Column(name = "PROC_NO_TO")
private Long procNoTo;
@Column(name = "PROC_BEGIN_DATE")
@org.hibernate.annotations.Type(type="com.dds.util.hibernate.DateMidnightType")
private DateMidnight procBeginDate;
@Column(name = "PROC_END_DATE")
@org.hibernate.annotations.Type(type="com.dds.util.hibernate.DateMidnightType")
private DateMidnight procEndDate;
//getters, setters, hashCode, equals, constructors....
}
UnbNetworkClinicProvider.java
Code:
//imports...
@Entity
@Table(name = "UNB_NETWORK_CLINIC_PROVIDER")
public class UnbNetworkClinicProvider implements Serializable {
@EmbeddedId
private NetworkClinicProviderCompoundKey key;
@Column(name = "CLINIC_NO",insertable=false,updatable=false)
private Long clinicNo;
@Column(name = "DELTA_BEGIN_DATE")
@org.hibernate.annotations.Type(type="com.dds.util.hibernate.DateMidnightType")
private DateMidnight deltaBeginDate;
@Column(name = "DELTA_END_DATE")
@org.hibernate.annotations.Type(type="com.dds.util.hibernate.DateMidnightType")
private DateMidnight deltaEndDate;
@OneToOne(fetch = FetchType.LAZY,mappedBy="clinicProvKey")
@org.hibernate.annotations.OptimisticLock(excluded=true)
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL})
private UnbClinicProv unbClinicProv;
//getters, setters, hashCode, equals, constructors....
}
NetworkClinicProviderCompoundKey.java
Code:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class NetworkClinicProviderCompoundKey implements Serializable {
@Column(name = "NETWORK_NO",insertable=false,updatable=false)
private Long networkNo;
@Column(name = "CLINIC_NO",insertable=false,updatable=false)
private Long clinicNo;
@Column(name = "PROVIDER_NO",insertable=false,updatable=false)
private Long providerNo;
//getters, setters, hashCode, equals, constructors....
}
ClinicProvCompoundKey.java
Code:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class ClinicProvCompoundKey implements Serializable {
@Column(name = "CLINIC_NO",insertable=false,updatable=false)
private Long clinicNo;
@Column(name = "PROVIDER_NO",insertable=false,updatable=false)
private Long providerNo;
//getters, setters, hashCode, equals, constructors....
}