论坛首页 Java企业应用论坛

one-to-one时出现的错误,请指教

浏览 3018 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-03-23  
我的类
public class PersonModel {
   
    private int id;
    private String name;
    private String address;
......
}

public class Account {
   
    private int person_id;
    private int account;
    private PersonModel person = null;
......
}

这两个类要建立一对一关联
请问映射文件应该怎么写
我是这样写的
<class name="src.mapping.PersonModel" table = "test_person">
<id name = "id" type = "int">
<generator class="sequence">
<param name="sequence">TEST_PERSON_ID_SEQ</param>
</generator>
</id>
<property name = "name"/>
<property name = "address"/>
</class>

<class name="src.mapping.Account" table = "test_account">
<id name = "person_id" type = "int">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name = "account" column = "account" type = "int"/>
<one-to-one name = "person" class = "src.mapping.PersonModel" constrained = "false" cascade = "none" />
</class>

程序
        PersonModel person = new PersonModel();
        person.setName("lyh");
        person.setAddress("nankai");
       
        Account account = new Account();
        account.setAccount(10);
        account.setPerson(person);
       
        try{
            sessionFactory = conf.buildSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(account);
            transaction.commit();
            session.close();  
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }

但是在执行到transaction.commit();时报错
SQL insert, update or delete failed (row not found)
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at src.TestPersonModel.oneToOne(TestPersonModel.java:145)
at src.TestPersonModel.main(TestPersonModel.java:44)

请问我在什么地方搞错了
   发表时间:2004-03-23  
是不是有外键约束?
0 请登录后投票
   发表时间:2004-03-23  
我将one-to-one那行改为
<one-to-one name = "person" class = "src.mapping.PersonModel" cascade = "all" constrained = "true"/>
同样是报这个错误
我的目的是在session.save(account);时在person表和account表中同时各写入一行相互关联的数据
请问该如何实现
0 请登录后投票
   发表时间:2004-03-25  
如果我把<one-to-one>中的级联选项去掉
就可以正常插入数值
但是这样就只能在两个表中分别插入数据
而且两个表也没有关联了
请问如何实现真正的一对一呢
我错在哪里
请指教
0 请登录后投票
   发表时间:2004-03-27  
去掉cascade,然后保存presonmodol
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics