我正在使用DB2数据库和hibernate框架。有两个表,如Word和Litter,它们的关系是一对多。
架构A:
Table Word: ID,
IDLIT -> references to ID of Litter table
Name
架构B:
Table Litter ID,
Short_Name,
Long_Name.
两个表都位于两个不同的模式中,但在一个数据库中。我有一个POJO和XML映射文件来映射表。现在,在处理多对一关系(Litter - Word)时,如何将对象保存到数据库中?有什么建议吗?如果是,请提前向我提供详细说明!
我也改变了,因为我的映射xml文件对于每个POJO类都不同。以下是运行代码:
Session session = HibernateUtility.getSessionFactory().openSession();
session.beginTransaction();
Word word = new Word();
Word word1 =new Word();
Litter litter = new Litter();
litter.setFullname("bla bla");
word.setLitter(litter);
word1.setLitter(litter); //Here I have to handle one to many relationship
Set Words =new HashSet();
Words.add(word);
Words.add(word1);
litter.setWords(words);
session.save(litter);
session.save(word);
session.save(word1);
session.getTransaction().commit();
session.close();
但是这段代码给出了异常:JDBCExceptionReporter logExceptions 我使用的方法是否正确?
Stack trace------------------ INFO: schema update complete Hibernate: select max(ID) from LITTER сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: -204, SQLState: 42704 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=BIMASH.LITTER, DRIVER=3.64.104 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: -204, SQLState: 42704 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=BIMASH.LITTER, DRIVER=3.64.104 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: -727, SQLState: 56098 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-204;42704;BIMASH.LITTER, DRIVER=3.64.104 Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:107) at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:44) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172) 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:535) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519) at Main.main(Main.java:73)
答案 0 :(得分:0)
您可以使用映射文件中schema
元素的<class>
属性定义映射类所属的模式。请注意,这将覆盖常规<hibernate-mapping>
的{{1}}属性中的设置。
schema
以下Hibernate参考章节可能值得一读:Chapter 5. Basic O/R Mapping,特别是关于<!-- A is set to be the default schema -->
<hibernate-mapping schema="A">
<class name="mypackage.Word" table="WORD" >
....
</class>
<!-- Overriding the schema definition for this class -->
<class name="mypackage.Litter" table="LITTER" schema="B">
....
</class>
</hibernate-mapping>
和<hibernate-mapping>
元素的部分。
看到堆栈跟踪,似乎在类中使用了<class>
。如果是这种情况(<generator class="increment">
查询似乎如此),您还必须在select max(ID) from LITTER
中指定架构,以便发布的查询转换为<generator>
。您可以使用select max(ID) from B.LITTER
中的<param>
schema
来实现这一目标:
<generator>
有关详细信息,请参阅此问题:Hibernate: ID generator using increment and Oracle Schema