求助各位大神:一对一,Hibernate 下正常,但是SSH下只能保存主表信息。
本帖最后由 u011327966 于 2013-12-10 10:11:20 编辑 大神,求助啊,从昨天一直整到现在,就是没找出错误!!!小弟在此表示感谢!
建表:
CREATE TABLE t_person
(
id INT PRIMARY KEY AUTO_INCREMENT,
userName VARCHAR(30)
)
CREATE TABLE t_card
(
id INT PRIMARY KEY ,
cardNum VARCHAR(30),
FOREIGN KEY(id) REFERENCES t_person(id)
)
实体类:
package com.lp.entity;
public class Person
{
private int id;
private String userName ;
private Card card;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public Card getCard()
{
return card;
}
public void setCard(Card card)
{
this.card = card;
}
}
package com.lp.entity;
public class Card
{
private int id;
private String cardNum;
private Person person ;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getCardNum()
{
return cardNum;
}
public void setCardNum(String cardNum)
{
this.cardNum = cardNum;
}
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
}
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lp.entity">
<class name="Person" table="t_person">
<id name="id">
<generator class="native" ></generator>
</id>
<property name="userName"></property >
<one-to-one name="card" class="Card"></one-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lp.entity">
<class name="Card" table="t_card">
<id name="id" column ="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="cardNum"></property >
<one-to-one name="person" class="Person"></one-to-one>
</class>
</hibernate-mapping>
如果单单使用Hibernate,可以正常保存:
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
person.setUserName("name");
Card card = new Card();
card.setCardNum("1990");
card.setPerson(person);
session.save(card);
transaction.commit();
但是在SSH下:
package com.lp.dao.test;
import org.hibernate.Session;
import com.lp.dao.IBaseDAO;
import com.lp.entity.Card;
import com.lp.entity.Person;
public class TestDAO extends IBaseDAO<Card, Integer>
{
public void testAdd()
{
Person person = new Person();
person.setUserName("name");
Card card = new Card();
card.setCardNum("1990");
card.setPerson(person);
Session session = getCurrentSession();
try
{
session.save(person);
System.out.println("**********************");
session.save(card);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Exception"+e.getMessage());
}
}
}
在SSH下,仅仅保存了Person,无法保存Card,也没有抛出任何异常,那个IBaseDAO我反复测过,没什么错,以前都是可以的,现在就是在这种一对一的情况无法保存。
[解决办法]
<one-to-one name="person" class="Person"></one-to-one>
加上cascade="all" 或者你只要级联保存或更新的话就cascade="save-update"
[解决办法]
设置级联保存。
[解决办法]
cascade 这个 没有加吧。
[解决办法]
如果SSH环境搭的没问题的话,加上cascade应该是没有问题的
[解决办法]
session.save(person);
System.out.println("**********************");
session.save(card);
一对一关系,只需要save一方即可,去掉一个