我在使用Hibernate + hsqldb的java應用程序時遇到了一些麻煩。 Here is database schema I created. 我使用hibernate.cfg.xml配置了Hibernate,並使用Intellij Idea的「Generate Persistance Mapping」功能爲它們生成了所有的pojos和映射。如何在複合主鍵中插入休眠
這些生成的文件:
的POJO:
User.java
public class User {
private int id;
private String name;
private String lastname;
private String patronym;
private Date birthdate;
private String email;
private String password;
private Set<Disk> disks;
private Set<Takenitem> takenDisks;
//setters, getters and constructors;
}
Disk.java
public class Disk {
private int id;
private String name;
private User host;
//setters, getters and constructors;
}
Takenitem.java
public class Takenitem {
private int iddisk;
private int iduser;
private User tempUser;
private Disk disk;
//setters, getters and constructors;
}
TakenitemPK.java(複合ID類)
public class TakenitemPK implements Serializable {
private int iddisk;
private int iduser;
//setters, getters and constructors;
}
映射:
User.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!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="com.dvdexchange.User" table="USER" schema="PUBLIC" catalog="PUBLIC">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" column="NAME"/>
<property name="lastname" column="LASTNAME"/>
<property name="patronym" column="PATRONYM"/>
<property name="birthdate" column="BIRTHDATE"/>
<property name="email" column="EMAIL"/>
<property name="password" column="PASSWORD"/>
<set name="disks" inverse="true">
<key>
<column name="IDHOST"/>
</key>
<one-to-many not-found="ignore" class="com.dvdexchange.Disk"/>
</set>
<set name="takenDisks" inverse="true">
<key>
<column name="IDUSER"/>
</key>
<one-to-many not-found="ignore" class="com.dvdexchange.Takenitem"/>
</set>
</class>
</hibernate-mapping>
Disk.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!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="com.dvdexchange.Disk" table="DISK" schema="PUBLIC" catalog="PUBLIC">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" column="NAME"/>
<many-to-one name="host" class="com.dvdexchange.User">
<column name="IDHOST"/>
</many-to-one>
</class>
</hibernate-mapping>
Takenitem.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!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="com.dvdexchange.Takenitem" table="TAKENITEM" schema="PUBLIC" catalog="PUBLIC">
<composite-id mapped="true" class="com.dvdexchange.TakenitemPK">
<key-property name="iddisk" column="IDDISK"/>
<key-property name="iduser" column="IDUSER"/>
</composite-id>
<many-to-one name="tempUser" class="com.dvdexchange.User" insert="false" update="false">
<column name="IDUSER"/>
</many-to-one>
<many-to-one name="disk" class="com.dvdexchange.Disk"/>
</class>
</hibernate-mapping>
一切工作正常,但是當我不能在Takenitem實現插入。 下面是代碼:
Main.java
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
UserDAO userDAO = new UserDAOHibernateImpl();
DiskDAO diskDAO = new DiskDAOHibernateImpl();
TakenItemDAO takenItemDAO = new TakenItemDAOHibernateImpl();
System.out.println("Application started:");
takenItemDAO.addBorrowing(1, 24);
}
}
TakenItemDAOHibernateImpl.java(。addBorrowing())
public void addBorrowing(int iduser, int iddisk) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Criteria crTempUser = session.createCriteria(User.class);
crTempUser.add(Restrictions.eq("id", iduser));
User tempUser = (User) crTempUser.uniqueResult();
Criteria crBorrowedDisk = session.createCriteria(Disk.class);
crBorrowedDisk.add(Restrictions.eq("id", iddisk));
Disk borrowedDisk = (Disk) crBorrowedDisk.uniqueResult();
Takenitem takenitem = new Takenitem(iduser, iddisk, tempUser, borrowedDisk);
session.save(takenitem);
tx.commit();
}catch (HibernateException e) {
if (tx != null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
當我運行我的節目,我有以下異常:
Hibernate: select this_.ID as ID1_2_0_, this_.NAME as NAME2_2_0_, this_.LASTNAME as LASTNAME3_2_0_, this_.PATRONYM as PATRONYM4_2_0_, this_.BIRTHDATE as BIRTHDAT5_2_0_, this_.EMAIL as EMAIL6_2_0_, this_.PASSWORD as PASSWORD7_2_0_ from PUBLIC.USER this_ where this_.ID=?
Hibernate: select this_.ID as ID1_0_0_, this_.NAME as NAME2_0_0_, this_.IDHOST as IDHOST3_0_0_ from PUBLIC.DISK this_ where this_.ID=?
Hibernate: insert into PUBLIC.TAKENITEM (disk, IDDISK, IDUSER) values (?, ?, ?)
мар 01, 2016 6:02:38 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: -5501, SQLState: 42501
мар 01, 2016 6:02:38 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: user lacks privilege or object not found: DISK
мар 01, 2016 6:02:38 PM org.hibernate.internal.SessionImpl$5 mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [could not prepare statement]
org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:78)
at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.buildBatchStatement(AbstractBatchImpl.java:136)
at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.getBatchStatement(AbstractBatchImpl.java:125)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2902)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3421)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
at com.dvdexchange.dao.impl.TakenItemDAOHibernateImpl.addBorrowing(TakenItemDAOHibernateImpl.java:31)
at com.dvdexchange.main.Main.main(Main.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: DISK
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
... 26 more
看看SQL日誌和此消息ERROR: user lacks privilege or object not found: DISK
。
我該如何解決?爲什麼Ii不工作。請幫幫我。
在此先感謝!
我應該改變什麼? Actualy表「takenitem」只有iddisk和iduser列。那麼,什麼是錯的?注意我的eddited方法代碼 – IngeniousTom
只要確保* .hbm.xml中引用的所有列名和表名都與數據庫表名和列名的名稱和大小寫匹配。 – Gopal00005
我很舒服。我的程序工作正常與本地URL,但是當我運行我的Hibernate與此URL:jdbc:hsqldb:mem:test然後我總是收到此消息,找不到一些對象(((( – IngeniousTom