如何配置主键和外键之间的OneToOne关系?

时间:2013-12-05 00:12:52

标签: java hibernate

我有两个表,一个表有id主键,另一个表键是外键。如何映射这些表之间的一对一关系。请注意。

表1

date_id(PK)
start_date
end_date
create_date
modify_date

表2

product_id(PK)
product_name
productversion
date_id(FK)

在Table-1 bean中配置OneToOne关系映射时,

private int date_id;
private ParoducttFeature paroductfeature;

的getter / setter

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="DATE_ID")
public int getDate_id() {
    return date_id;
}

 @OneToOne(fetch = FetchType.LAZY, mappedBy = "daterange", cascade = CascadeType.ALL)
public ProductFeature getParoductfeature() {
    return productfeature;
}


public void setProductFeature(ProductFeature productfeature) {
    this.productfeature= productfeature;
}

另一个table-2 bean:

private DateRange daterange;

   @OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="date_id")
public DateRange getDaterange() {
    return daterange;
}

/**
 * @param daterange the daterange to set
 */
public void setDaterange(DateRange daterange) {
    this.daterange = daterange;
}

我收到错误无法插入空值。请告知为什么插入空值。

错误堆栈跟踪:

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'Date_id', table 'Test.dbo.ProductFeature'; column does not allow nulls. INSERT fails.
    com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
    com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
    com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
    org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2989)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3501)
    org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:393)
    org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:227)
    org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:207)
    org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:191)
    org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
    org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
    org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java

Hibernate会话代码:

Session session = (Session) HibernateUtil.getSessionFactory().openSession();

dateRange.setStart_date(date1);
dateRange.setEnd_date("12/31/2099");
dateRange.setCreate_date(date1);
dateRange.setModify_date(date1);
session.save(dateRange);

productFeature.setproductname("stack");
productfeature.setproductversion("1.23");
session.save(productFeature);
session.getTransaction().commit();
session.flush();
HibernateUtil.shutdown();

1 个答案:

答案 0 :(得分:1)

数据库中的ProductFeature.Date_id列有一个非空约束。此列映射到属性ProductFeature.daterange。当您在代码中插入ProductFeature时,其daterange属性为null。因此,您有效地将null插入到已配置为拒绝空值的列中。所以你得到了这个例外。

如果你添加

productFeature.setDaterange(dateRange);

在保存productFeature之前,它应该可以正常工作。