taitoushan
2009-11-20 10:00
浏览 285
已采纳

hibernate一对一关联关系映射的问题

[color=red]定义一个人的域对象:[/color]
[code="java"]public class Person {
private int id;

private String name;

private IdCard idCard;

}[/code]

[color=red]定义一个该人的身份证的域对象:[/color]
[code="java"]public class IdCard {
private int id;

private Date usefuldate;

private Person person;

}[/code]

[color=red]Person.hbm.xml配置文件:[/color]
[code="java"]



PERSON_SEQUENCE





[/code]

[color=red]IdCard.hbm.xml配置文件:[/color]
[code="java"]



person





[/code]

工具类里的保存方法:
[code="java"] public static void save(Object entity) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(entity);
tx.commit();
} finally {
if (session != null)
session.close();
}
}[/code]

测试方法:
[code="java"] public static void main(String[] args) {
Person p = new Person();
p.setName("idcard test");

    IdCard idCard = new IdCard();
    p.setIdCard(idCard);
    idCard.setUsefuldate(new Date());   
    idCard.setPerson(p);

    PersonDao persondao = new PersonDaoImpl();
    persondao.savePerson(p);

    IdCardDao idcarddao = new IdCardDaoImpl();
    idcarddao.addIdCard(idCard);

}[/code]

异常信息:
[code="java"]Hibernate: select PERSON_SEQUENCE.nextval from dual
Hibernate: insert into person (name, id) values (?, ?)
Hibernate: insert into idcard (usefuldate, id) values (?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.atwasoft.utils.HibernateUtil.save(HibernateUtil.java:37)
at com.atwasoft.dao.impl.IdCardDaoImpl.addIdCard(IdCardDaoImpl.java:10)
at com.atwasoft.test.Test.main(Test.java:30)
Caused by: java.sql.BatchUpdateException: ORA-02291: 违反完整约束条件 (TEST.IDCARD_FK) - 未找到父项关键字

at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:342)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10656)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)[/code]

[color=red][size=medium]数据库里的idcard表的ID既做主键,又做外键(参考Person的主键ID),怎么还是报找不到父项关键字呢[/size][/color]

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题