red110 2010-06-30 16:15
浏览 547
已采纳

Hibernate 不能插入数据问题[不允许从数据类型 datetime 到数据类型 timestamp 的隐性转换]

小弟新手,在学习hibernate时碰到如下问题
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into oa_employee (name, birthdayDate, id) values (?, ?, ?)
org.hibernate.exception.GenericJDBCException: could not insert: [com.wl.Employee.Employee]
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:2267)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.wl.hibernate.test.UserTest.test1(UserTest.java:44)
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 junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]不允许从数据类型 datetime 到数据类型 timestamp 的隐性转换(表 'oa.dbo.oa_employee',列 'birthdayDate')。请使用 CONVERT 函数来运行此查询。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2247)
... 29 more

Employee.java
[code="java"]

package com.wl.Employee;
import java.util.Date;

public class Employee {
private String id ;
private String name;
private Date birthday;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

[/code]

ExportDB.java
[code="java"]
package com.wl.hibernate.util;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

public static void main(String[] args) {

//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

export.create(true, true);
}
}
[/code]
Employee.hb.xml

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









[/code]

hibernate.cfg.xml

[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">



true

com.microsoft.jdbc.sqlserver.SQLServerDriver


jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=oa

sa
sa

<property name="hibernate.dialect">
    org.hibernate.dialect.DB2Dialect
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/wl/Employee/Employee.hb.xml" />


[/code]

UserTest.java
[code="java"]
package com.wl.hibernate.test;

import java.sql.Timestamp;
import java.util.List;
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import junit.framework.TestCase;

import com.wl.Employee.Employee;
import com.wl.hibernate.util.*;

public class UserTest extends TestCase {
public static String EmployeeID = "" ;
public void test1(){
Session session = null ;
Transaction tx = null;
Employee Employee = null ;
try{
session = HibernateUtil.getSession();
tx = session.getTransaction();
tx.begin();
Employee = new Employee();
Employee.setName("王五");
Employee.setBirthday(new Timestamp(System.currentTimeMillis()));
session.save(Employee);
Employee = new Employee();
Employee.setName("赵六");
Employee.setBirthday(new Timestamp(System.currentTimeMillis()));
session.save(Employee);
Employee = new Employee();
Employee.setName("张三");
Employee.setBirthday(new Timestamp(System.currentTimeMillis()));
session.save(Employee);

        Employee = new Employee();
        Employee.setName("李四");
        Employee.setBirthday(new Timestamp(System.currentTimeMillis()));
        session.save(Employee);

        this.setEmployeeID(Employee.getId());
        tx.commit();
    }catch(Exception e){
        e.printStackTrace();
        tx.rollback();
    }finally{
        if(session!=null){
            HibernateUtil.closeSession(session);
        }
    }
}
public static void setEmployeeID(String employeeID){
    EmployeeID = employeeID ;
}
public static String getEmployeeID(){
    return EmployeeID ;
}

}
[/code]

无论setBirthday()中的参数是Date还是Timestamp类型全部报一样的错误,各位高手下小弟感激不尽

  • 写回答

3条回答 默认 最新

  • myali88 2010-06-30 17:14
    关注

    为什么你上面的配置中hibernate.dialect采用DB2Dialect而不是SQLServerDialect?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗