在单个休眠会话中执行两次保存(“entity-name”,object)?

时间:2017-01-03 14:34:02

标签: java hibernate

我有一个POJO类NewUniversity.java。我为它创建了一个映射文件。 RegisterAction.hbm.xml。现在问题是当我尝试从DAO类调用save方法时,只有第一个被执行,第二个没有。我通过xml文件中的'entity-mapping'属性将两个表链接到一个实体。

文件:NewUniversity.java

public class NewUniversity implements Serializable {

// University fields
private String uCode;
private String uName;
private String uAddress;
private String uCity;
private String uState;
private long uContactNo;
private String uEmail;

// University director fields
private String name;
private String address;
private String city;
private String state;
private long contactNo;
private String email;
private String userName;
private String pwd;

public String getuCode() {
    return uCode;
}

public void setuCode(String uCode) {
    this.uCode = uCode;
}

public String getuName() {
    return uName;
}

public void setuName(String uName) {
    this.uName = uName;
}

public String getuAddress() {
    return uAddress;
}

public void setuAddress(String uAddress) {
    this.uAddress = uAddress;
}

public String getuCity() {
    return uCity;
}

public void setuCity(String uCity) {
    this.uCity = uCity;
}

public String getuState() {
    return uState;
}

public void setuState(String uState) {
    this.uState = uState;
}

public long getuContactNo() {
    return uContactNo;
}

public void setuContactNo(long uContactNo) {
    this.uContactNo = uContactNo;
}

public String getuEmail() {
    return uEmail;
}

public void setuEmail(String uEmail) {
    this.uEmail = uEmail;
}


// University directors setters/getters
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public long getContactNo() {
    return contactNo;
}

public void setContactNo(long contactNo) {
    this.contactNo = contactNo;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}

}

文件:RegisterAction.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD  3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>
<class name="in.ryr.register.NewUniversity" table="university"
    entity-name="university">
    <id name="uCode" column="code" type="string">
        <generator class="assigned" />
    </id>
    <property name="uName" column="name" />
    <property name="uAddress" column="address" />
    <property name="uCity" column="city" />
    <property name="uState" column="state" />
    <property name="uContactNo" column="contact_no" />
    <property name="uEmail" column="email" />
    <property name="userName" column="username" />
</class>

<class name="in.ryr.register.NewUniversity" table="university_director"
    entity-name="uniDirector">
    <id name="userName" column="username" type="string">
        <generator class="assigned" />
    </id>
    <property name="name" column="name" />
    <property name="address" column="address" />
    <property name="city" column="city" />
    <property name="state" column="state" />
    <property name="contactNo" column="contact_no" />
    <property name="email" column="email" />
    <property name="pwd" column="password" />
    <property name="uCode" column="u_code" />
</class>
</hibernate-mapping>

档案:RegisterDAO.java

public void register(NewUniversity user) throws Exception {
    session = factory.openSession();
    transaction = session.beginTransaction();
    try {
        session.save("university", user);   //This is working fine
        session.save("uniDirector", user);  //This doesn't execute at all
        transaction.commit();
        session.close();
    } catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
        session.close();
    }
}

我也尝试过使用persist()。但没有运气! 关键是,当我在两个不同的会话上执行它时,它完美地工作。 (所有对象值都通过.jsp到struts)。由于某些原因,我没有使用注释。

1 个答案:

答案 0 :(得分:0)

也许是因为Session#save(entityName, object)方法要求object成为&#34;瞬态&#34;对象(即尚未保留的对象),并且在第一次调用session.save("university", user);之后,user不再是短暂的。

  

保留给定的瞬态实例,首先分配生成的   标识符。 (或者使用标识符属性的当前值if   使用指定的生成器。)此操作级联到关联   如果关联使用cascade =&#34; save-update&#34;

进行映射,则为实例

当你获得坚持不懈的大学时会发生什么?#34;在第一个save之后的实体,然后将其保存为&#34; uniDirector&#34; ?