Hi,
I am having difficulty setting up a One to One mapping using Hibernate Annotations where there is a composite key. I have the following classes and annotations:
StoreServer.java (getters/setters and toString ommitted for brevity)
Code:
@Entity
@IdClass(StoreServerPK.class)
@Table(name="Store_Server")
public class StoreServer implements Serializable {
/**
* Serializable class - generated UID
*/
private static final long serialVersionUID = -5410564578856243437L;
@Id
@Column(name="STORE_NO", nullable=false)
private int storeNumber;
@Id
@Column(name="SERVER_NO", nullable=false)
private String serverNumber;
@Column(name="IP_ADDRESS", nullable=false)
private String ipAddress;
@Column(name="DOMAIN_NAME", nullable=false)
private String domainName;
@OneToOne(mappedBy="storeServer", cascade=CascadeType.ALL)
@JoinColumn(name="SERVER_NO")
private Server server;
StoreServerPK.java (hashCode and equals ommitted for brevity)
Code:
public class StoreServerPK implements Serializable {
/**
* Serializable class - generated UID
*/
private static final long serialVersionUID = -1401889029390423604L;
protected int storeNumber;
protected String serverNumber;
/**
* No-arg constructor
*/
public StoreServerPK(){}
/**
* Constructor
*
* @param storeNumber the store number to set
* @param serverNumber the server number to set
*/
public StoreServerPK(int storeNumber, String serverNumber) {
this.storeNumber = storeNumber;
this.serverNumber = serverNumber;
}
Store.java (getters/setters and toString omitted for brevity)
Code:
@Entity
@Table(name="Server")
public class Server implements Serializable {
/**
* Serializable class - generated UID
*/
private static final long serialVersionUID = 8182286717903731330L;
@Id
@Column(name = "SERVER_NO", unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy = "foreign", parameters={
@Parameter(name="property", value="storeServer")
})
private String serverNumber;
@Column(name="SERVER_DESCRIPTION", nullable=true)
private String serverDescription;
@Column(name="SERVER_MAKE", nullable=true)
private String serverMake;
@Column(name="SERVER_MODEL", nullable=true)
private String serverModel;
@Column(name="SERVER_OS_MAKE", nullable=true)
private String serverOSMake;
@Column(name="SERVER_OS_MODEL", nullable=true)
private String serverOSModel;
@OneToOne
@PrimaryKeyJoinColumn
private StoreServer storeServer;
The error I see in the log is as follows:
Code:
Caused by: org.hibernate.MappingException: Foreign key (FK93671B638C748C3C:Server [SERVER_NO])) must have same number of columns as the referenced primary key (Store_Server [SERVER_NO,STORE_NO])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1354)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1261)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:383)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
I can see what the problem is, but I am not sure how to fix it. Can you please tell me what steps I need to take to resolve this issue?
Thank you!