Hibernate update()在子类中创建新记录而不是更新它

时间:2017-02-13 13:39:41

标签: java hibernate annotations one-to-one

我有2个班级'员工' &安培; '帐户'与 OneToOne之间存在关系。员工有帐户。当我尝试编辑任何记录时,会在Account(子)表中创建新记录而不是更新。

员工类:

    @Entity
@Table(name="employee")
public class Employee 
{
@Id
@Column(name="empid")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="emp_seq")
@SequenceGenerator(
            name="emp_seq",
            sequenceName="emp_sequence",
            allocationSize=1
            )
long empId;

    @Column(name="name")
    String name;

    @Column(name="email")
    String email;

    @Column(name="city")
    String city;

    @Column(name="dob")
    Date DOB;

    @Column(name="gender")
    String gender;

    @Column(name="address")
    String address;

    @Column(name="state")
    String state;

    @Column(name="country")
    String country;

    @Column(name="mobile")
    String mobile;

    @Access(AccessType.PROPERTY)
    @OneToOne(targetEntity=Account.class, cascade=CascadeType.ALL)
    Account account;


    public Account getAccount() {
        return account;
    }
    public void setAccount(Account finance) {
        this.account = finance;
    }

    public long getEmpId() {
        return empId;
    }
    public void setEmpId(long empId) {
        this.empId = empId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getDOB() {
        return DOB;
    }
    public void setDOB(Date dOB) {
        DOB = dOB;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    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 String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}

帐户类:

 @Entity
@Table(name="account")
public class Account 
{
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="acc_seq")
    @SequenceGenerator(
            name="acc_seq",
            sequenceName="acc_sequence",
            allocationSize=1
            )
    long accountNo;

    String basic;
    String ta;
    String da;
    String boa;
    String total;

    public long getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(long accountNo) {
        this.accountNo = accountNo;
    }

    public String getBasic() {
        return basic;
    }
    public void setBasic(String basic) {
        this.basic = basic;
    }
    public String getTa() {
        return ta;
    }
    public void setTa(String ta) {
        this.ta = ta;
    }
    public String getDa() {
        return da;
    }
    public void setDa(String da) {
        this.da = da;
    }
    public String getBoa() {
        return boa;
    }
    public void setBoa(String boa) {
        this.boa = boa;
    }
    public String getTotal() {
        return total;
    }
    public void setTotal(String total) {
        this.total = total;
    }

}

我尝试使用saveOrUpdate(),save()& update()方法,但得到相同的错误。

如何解决此问题?

hibernate cfg xml:

    <?xml version='1.0' encoding='UTF-8'?>  
 <!DOCTYPE hibernate-configuration SYSTEM 
    "hibernate-configuration-3.0.dtd">

  <hibernate-configuration>  
    <session-factory>  
        <property name="hbm2ddl.auto">update</property>  

        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>

        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Employee_Mgmt</property>  
        <property name="hibernate.connection.username">postgres</property>  
        <property name="hibernate.connection.password">root</property>  

        <property name="show_sql">true</property>

    <!--<mapping resource="sampleConfig.xml"/>  
    -->
    <mapping class="org.empmgmt.model.Employee"/>
    <mapping class="org.empmgmt.model.Project"/>
    <mapping class="org.empmgmt.model.Account"/>
    <mapping class="org.empmgmt.model.UserBean"/>
    </session-factory>  

</hibernate-configuration> 

主要课程:

    public class EmployeeDao 
{
    private Session session;
    private Transaction transaction;
    private ConnectDB connectDB;    
    private Employee employee;
    private static Logger logger;

    public EmployeeDao() 
    {
        connectDB = new ConnectDB();
        session = connectDB.getSession();
        logger = Logger.getLogger("EmployeeManagementlogger");
    }

    public void updateEmployee(Employee employee)
    {
        try 
        {   
            transaction = session.beginTransaction();

            logger.debug("Session is active & transaction is started");

            session.update(employee);
            //session.saveOrUpdate(employee);
            //session.merge(employee);
            transaction.commit();
        } 

        catch (Exception e) 
        {
            logger.debug("Exception occurred");
            e.printStackTrace();
        }

        finally 
        {
            session.close();
            logger.debug("Session is closed");
        }
    }
}

0 个答案:

没有答案