我有以下设置: Windows 7的 MSSQL数据库 冬眠
使用odbcad32.exe作为数据源注册MSSQL Server!
我有以下代码:(只有重要部分!)
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);”出了问题。