[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]
参考实例详解
http://www.iteye.com/topic/450705
http://www.iteye.com/topic/450837
..
我也是初学
不过 貌似你的标签有点问题
这里为什么只配置一个name
配置关联关系属性的时候 要有四部分组成
name -- 关系属性的名称
class -- 关系属性所对应的类 hibernate同类来锁定表
cascade -- 级联
还有一个是外键
在 one-to-one 标签中 默认的就是共享主外键 外键就不用配置了
正确的应该是这样的
还有你的dao部分
public static void save(Object entity) {
Session session = null;
Transaction tx = null;
try {
/*
*在这里你直接是getSession();
*我不请求你的util是怎么写的不过看你下面又把session给close了
* 那么你应该是调用的OpenSession()方法了
* 为什么不用SessionFactory的getCurrentSession方法呢?
* 建议你在HibernateUtil种提供一个getCurrentSession()的方法
* 这是hibernate提供的线程安全的方法 建议你看一下ThreadLocal
* 还有如果使用了CurrentSession 需要在hibernate的配置文件中加上
* 如下标签
* * name="current_session_context_class">thread
*/
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
session.save(entity);
tx.commit();
} catch(RuntimeException e){
e.printstacktrace;
if(tx!=null){
tx.rollback();
}
}
}
小弟是第一次回答问题 不求给分
回答的有不对的地方 请高手指出 不吝赐教
hibernate配置文件的时候是跟数据库一一对应的应该把你数据库中所有的字段都写到property里面
<generator class="sequence">
<param name="sequence">PERSON_SEQUENCE</param>
</generator>
你把这里面的PERSON_SEQUENCE改写下试试
我一般都是写成这样的[table]
[/table]