你的位置:首页 > Java教程

[Java教程]Hibernate处理没有主键的table

      之前用Hibernate映射的表一直都有主键,刚好今天测试的是一张无主键的表,一直报错无法匹配,查了半天原来Hibernate是一定要设置主键了,但是是不是不设置主键就没法处理了?当然不是,可以通过设置复合主键的方式来处理,当然企业环境开发中,所有的表肯定是有主键的,这里只是做一个记录,好了  废话不多少了,开始吧!

     本人用的工具开发工具是IDEA+Hibernate4.x+sqlserver,其中涉及到的文件有EmpEntity.java,EmpEntity.hbm.

这个三个文件都可以自动生成,这里就不截图了哈,如果不会的同志可以留言,下面分别贴下这三个文件

1、EmpEntity.hbm.<??><!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.

<??><!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./> <!-- 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. [email protected]@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. 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

 

以上,感谢观看,有问题请留言