1

我有以下设置:Windows 7 MSSQL 数据库休眠

MSSQL Server 是使用 odbcad32.exe 作为数据源注册的!

我有以下代码:(只有重要的部分!)

Person p = new Person("Hans", "Muster");
EntityManager eManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
eManager.getTransaction().begin();
eManager.persist(p); // when I delete this line, the program works!
Person geladen = eManager.find(Person.class, new Long(1));
eManager.getTransaction().commit();
eManager.close();

我有以下persistence.xml:(只有重要的部分!)

<properties>
  <property name="hibernate.connection.url" value="jdbc:odbc:MSSQL;database=hibernatedb"/>
  <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
  <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
  <property name="hibernate.connection.username" value="hibernate"/>
  <property name="hibernate.connection.password" value="geheim"/>
  <property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>

架构已正确创建。当我运行 Java 代码时,我得到以下异常。

Exception in thread "main" java.lang.UnsupportedOperationException
    at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(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.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy7.prepareStatement(Unknown Source)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:105)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:103)
    at org.hibernate.id.IdentityGenerator$InsertSelectDelegate.prepare(IdentityGenerator.java:137)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2870)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3381)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:840)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:815)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:819)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:865)
    at TestApp.main(TestApp.java:19)

当我删除第 19 行时:“eManager.persist(p);”。程序运行无异常。

我认为与数据库的连接有问题。我应该有写权限,因为数据库表是正确创建的。可以使用“Person geladen = eManager.find(Person.class, new Long(1));”从数据库中加载对象 只有“eManager.persist(p);” 造成问题。

4

0 回答 0