首先说一下,小弟想用hibernate保存实体到数据库,但是出了一些问题,我用的sql server数据库,为表创建了存储过程和触发器。
数据库有三个表: student , score ,
为student表创建了一个 Insert 触发器,当有新纪录添加到该表的时候,自动向表中添加一条日志信息
这是我的代码1.实体l类public class Student implements java.io.Serializable { // Fields private Integer id;
private String sname;
private Integer sex;
private Set scores = new HashSet(0); // Constructors /** default constructor */
public Student() {
} /** minimal constructor */
public Student(String sname) {
this.sname = sname;
} /** full constructor */
public Student(String sname, Integer sex, Set scores) {
this.sname = sname;
this.sex = sex;
this.scores = scores;
}
public Student(String sname, Integer sex) {
this.sname = sname;
this.sex = sex; } // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getSname() {
return this.sname;
} public void setSname(String sname) {
this.sname = sname;
} public Integer getSex() {
return this.sex;
} public void setSex(Integer sex) {
this.sex = sex;
} public Set getScores() {
return this.scores;
} public void setScores(Set scores) {
this.scores = scores;
}}2.测试类:
public static void main(String[] args) {
// TODO Auto-generated method stub //读取并解析配置文件
Configuration conf = new Configuration().configure();
//读取并解析映射文件兮兮
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession(); //打开session
Transaction tx = null;
try{
tx = session.beginTransaction();//开始一个事务
Student student = new Student("王青",Integer.valueOf(1));
session.save(student);
tx.commit(); //提交事务
}catch(Exception e){
if(tx != null)tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
3.映射文件:student.hbm.xlm
<hibernate-mapping>
<class name="com.DAO.Student" table="student" schema="dbo" catalog="jj">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="sname" type="java.lang.String">
<column name="sname" length="70" not-null="true" />
</property>
<property name="sex" type="java.lang.Integer">
<column name="sex" />
</property>
<set name="scores" inverse="true">
<key>
<column name="sid" not-null="true" />
</key>
<one-to-many class="com.DAO.Score" />
</set>
</class>
</hibernate-mapping>4.映射文件:score.hbm.xml <hibernate-mapping>
<class name="com.DAO.Score" table="score" schema="dbo" catalog="jj">
<composite-id name="id" class="com.DAO.ScoreId">
<key-property name="id" type="java.lang.Integer">
<column name="ID" />
</key-property>
<key-many-to-one name="student" class="com.DAO.Student">
<column name="sid" />
</key-many-to-one>
<key-property name="scores" type="java.lang.Integer">
<column name="scores" />
</key-property>
</composite-id>
<many-to-one name="student" class="com.DAO.Student" update="false" insert="false" fetch="select">
<column name="sid" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
5.映射文件.hbm.xnl:
<hibernate-mapping>
<class name="com.DAO.Mark" table="" schema="dbo" catalog="jj">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="tg" type="java.lang.String">
<column name="tg" length="110" />
</property>
<property name="time" type="java.util.Date">
<column name="time" length="23" />
</property>
</class>
</hibernate-mapping>
数据库有三个表: student , score ,
为student表创建了一个 Insert 触发器,当有新纪录添加到该表的时候,自动向表中添加一条日志信息
这是我的代码1.实体l类public class Student implements java.io.Serializable { // Fields private Integer id;
private String sname;
private Integer sex;
private Set scores = new HashSet(0); // Constructors /** default constructor */
public Student() {
} /** minimal constructor */
public Student(String sname) {
this.sname = sname;
} /** full constructor */
public Student(String sname, Integer sex, Set scores) {
this.sname = sname;
this.sex = sex;
this.scores = scores;
}
public Student(String sname, Integer sex) {
this.sname = sname;
this.sex = sex; } // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getSname() {
return this.sname;
} public void setSname(String sname) {
this.sname = sname;
} public Integer getSex() {
return this.sex;
} public void setSex(Integer sex) {
this.sex = sex;
} public Set getScores() {
return this.scores;
} public void setScores(Set scores) {
this.scores = scores;
}}2.测试类:
public static void main(String[] args) {
// TODO Auto-generated method stub //读取并解析配置文件
Configuration conf = new Configuration().configure();
//读取并解析映射文件兮兮
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession(); //打开session
Transaction tx = null;
try{
tx = session.beginTransaction();//开始一个事务
Student student = new Student("王青",Integer.valueOf(1));
session.save(student);
tx.commit(); //提交事务
}catch(Exception e){
if(tx != null)tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
3.映射文件:student.hbm.xlm
<hibernate-mapping>
<class name="com.DAO.Student" table="student" schema="dbo" catalog="jj">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="sname" type="java.lang.String">
<column name="sname" length="70" not-null="true" />
</property>
<property name="sex" type="java.lang.Integer">
<column name="sex" />
</property>
<set name="scores" inverse="true">
<key>
<column name="sid" not-null="true" />
</key>
<one-to-many class="com.DAO.Score" />
</set>
</class>
</hibernate-mapping>4.映射文件:score.hbm.xml <hibernate-mapping>
<class name="com.DAO.Score" table="score" schema="dbo" catalog="jj">
<composite-id name="id" class="com.DAO.ScoreId">
<key-property name="id" type="java.lang.Integer">
<column name="ID" />
</key-property>
<key-many-to-one name="student" class="com.DAO.Student">
<column name="sid" />
</key-many-to-one>
<key-property name="scores" type="java.lang.Integer">
<column name="scores" />
</key-property>
</composite-id>
<many-to-one name="student" class="com.DAO.Student" update="false" insert="false" fetch="select">
<column name="sid" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
5.映射文件.hbm.xnl:
<hibernate-mapping>
<class name="com.DAO.Mark" table="" schema="dbo" catalog="jj">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="tg" type="java.lang.String">
<column name="tg" length="110" />
</property>
<property name="time" type="java.util.Date">
<column name="time" length="23" />
</property>
</class>
</hibernate-mapping>
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at com.DAO.Test.main(Test.java:25)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 必须执行该语句才能获得结果。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getGeneratedKeys(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.util.GetGeneratedKeysHelper.getGeneratedKey(GetGeneratedKeysHelper.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1970)
... 15 more
hibernate 配置文件配置连接有问题 好像是。 连接不到数据库
错误肯定出在触发器和Hibernate的配合使用上。
如果不是绕不过去,建议不要在Hibernate管理的表上使用触发器。