Hibernate處理沒有主鍵的table


      之前用Hibernate映射的表一直都有主鍵,剛好今天測試的是一張無主鍵的表,一直報錯無法匹配,查了半天原來Hibernate是一定要設置主鍵了,但是是不是不設置主鍵就沒法處理了?當然不是,可以通過設置復合主鍵的方式來處理,當然企業環境開發中,所有的表肯定是有主鍵的,這里只是做一個記錄,好了  廢話不多少了,開始吧!

     本人用的工具開發工具是IDEA+Hibernate4.x+sqlserver,其中涉及到的文件有EmpEntity.java,EmpEntity.hbm.xml,hibernate.cfg.xml

這個三個文件都可以自動生成,這里就不截圖了哈,如果不會的同志可以留言,下面分別貼下這三個文件

1、EmpEntity.hbm.xml
<?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>
    <!--無主鍵處理方法-->
    <class name="springtest.hibernate.EmpEntity" table="emp" schema="dbo" catalog="model">
        <composite-id>
            <key-property name="empno" column="empno"></key-property>
            <key-property name="ename" column="ename" > </key-property>
            <key-property name="job" column="job" ></key-property>
            <key-property name="hiredate" column="hiredate" ></key-property>
            <key-property name="sal" column="sal" ></key-property>
            <key-property name="comm" column="comm" ></key-property>
            <key-property name="clob" column="clob" ></key-property>
        </composite-id>
    </class>
</hibernate-mapping>

 

2、hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=model</property>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.username">sa</property>
    <property name="connection.password">starlin.cn</property>
    <mapping class="springtest.hibernate.EmpEntity"/>
    <mapping resource="springtest/EmpEntity.hbm.xml"/>
    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

3、EmpEntity.java  注意:一定要序列化,不然會報錯的

package springtest.hibernate;

import javax.persistence.*;
import java.io.Serializable;

/**
 * Created by starlin
 * on 2015/12/12 20:57.
 */
@Entity
@Table(name = "emp", schema = "dbo", catalog = "model")
public class EmpEntity implements Serializable{
    private String empno;
    private String ename;
    private String job;
    private String hiredate;
    private String sal;
    private String comm;
    private String clob;
    @Basic
    @Column(name = "empno")
    public String getEmpno() {
        return empno;
    }

    public void setEmpno(String empno) {
        this.empno = empno;
    }

    @Basic
    @Column(name = "ename")
    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    @Basic
    @Column(name = "job")
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Basic
    @Column(name = "hiredate")
    public String getHiredate() {
        return hiredate;
    }

    public void setHiredate(String hiredate) {
        this.hiredate = hiredate;
    }

    @Basic
    @Column(name = "sal")
    public String getSal() {
        return sal;
    }

    public void setSal(String sal) {
        this.sal = sal;
    }

    @Basic
    @Column(name = "comm")
    public String getComm() {
        return comm;
    }

    public void setComm(String comm) {
        this.comm = comm;
    }

    @Basic
    @Column(name = "clob")
    public String getClob() {
        return clob;
    }

    public void setClob(String clob) {
        this.clob = clob;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EmpEntity empEntity = (EmpEntity) o;

        if (empno != null ? !empno.equals(empEntity.empno) : empEntity.empno != null) return false;
        if (ename != null ? !ename.equals(empEntity.ename) : empEntity.ename != null) return false;
        if (job != null ? !job.equals(empEntity.job) : empEntity.job != null) return false;
        if (hiredate != null ? !hiredate.equals(empEntity.hiredate) : empEntity.hiredate != null) return false;
        if (sal != null ? !sal.equals(empEntity.sal) : empEntity.sal != null) return false;
        if (comm != null ? !comm.equals(empEntity.comm) : empEntity.comm != null) return false;
        if (clob != null ? !clob.equals(empEntity.clob) : empEntity.clob != null) return false;


        return true;
    }

    @Override
    public int hashCode() {
        int result = empno != null ? empno.hashCode() : 0;
        result = 31 * result + (ename != null ? ename.hashCode() : 0);
        result = 31 * result + (job != null ? job.hashCode() : 0);
        result = 31 * result + (hiredate != null ? hiredate.hashCode() : 0);
        result = 31 * result + (sal != null ? sal.hashCode() : 0);
        result = 31 * result + (comm != null ? comm.hashCode() : 0);
        result = 31 * result + (clob != null ? clob.hashCode() : 0);
        return result;
    }


}

4、最后一個測試類Test.java

package springtest.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * Created by starlin
 * on 2015/12/11 22:58.
 */
public class Test {
    public static void main(String[] args) {
        EmpEntity empEntity = new EmpEntity();
        empEntity.setEmpno("9000");
        empEntity.setEname("hibernate");
        empEntity.setJob("test");
        empEntity.setHiredate("2015-12-12");
        empEntity.setSal("3000");
        empEntity.setComm("sfsf");
        empEntity.setClob("clob");
        //實例化Configuration,這行代碼默認加載hibernate.cfg.xml文件
        Configuration configuration = new Configuration().configure();
        //以Configuration創建SessionFactory
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //實例化session
        Session session = sessionFactory.openSession();
        //開始事物
        Transaction transaction = session.beginTransaction();
        //保存消息
        session.save(empEntity);
        //提交事物
        transaction.commit();
        //關閉session
        session.close();
    }
}
 

5、測試log

QQ截圖20151212221117

 

6、數據庫成功插入

image

 

以上,感謝觀看,有問題請留言


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM