我正在使用Hibernate 4.1.7 Java和Spring,使用MySql。
我有一个Publication类,它是抽象的,有3个子类,还有一个Page类,它也是抽象的,有2个子类。
在出版物中有一个List,在页面i中有一个出版物,所以它是一个常见的OneToMany / ManyToOne双向关系,但由于我需要对页面进行排序,我使用的是交叉表,如下所示:
// Publication.java
@OneToMany(fetch=FetchType.LAZY)
@JoinTable(name="publication_pages",
joinColumns=@JoinColumn(table="publication_pages", name="pub_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(table="publication_pages", name="pg_id", referencedColumnName="id"))
@OrderColumn(name="i")
public List<Page> getPages() {
return pages;
}
// Page.java
@ManyToOne
@JoinTable(name="publication_pages",
joinColumns=@JoinColumn(table="publication_pages", name="pg_id", referencedColumnName="id", insertable=false, updatable=false),
inverseJoinColumns=@JoinColumn(table="publication_pages", name="pub_id", referencedColumnName="id", insertable=false, updatable=false))
public Publication getPublication() {
return publication;
}
Hibernate不会抱怨注释,但是一旦我尝试调用getPages,它就会生成一个查询页面子类表上的pub_id的查询,而不是仅在交叉表中查找。
错误是:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'page1_.pub_id' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2318)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy54.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1897)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1698)
at org.hibernate.loader.Loader.doQuery(Loader.java:832)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:263)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2094)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:678)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1801)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:524)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:520)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:125)
at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:152)
at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:139)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:138)
at org.hibernate.collection.internal.PersistentList.isEmpty(PersistentList.java:121)
我做错了什么,或者是Hibernate错误,还是因为它在查询中执行了大量的联合而根本不可能?
TIA
编辑:这是hibernate生成的查询(我删除了所有不相关的列):Hibernate:
select
pages0_.pub_id as pub1_46_3_,
pages0_.pg_id as pg2_43_3_,
pages0_.i as i3_,
page1_.id as id42_0_,
[...]
page1_.pub_id as pub1_43_0_ [...]
from publication_pages pages0_
inner join (
select id, [...] from page_fixed union select id, [...] from page_query
) page1_
on pages0_.pg_id=page1_.id left outer join (
select id, [...] from publication
) publicatio2_
on page1_.pub_id=publicatio2_.id left where pages0_.pub_id=?
如您所见,它在连接中使用page1_.pub_id(就像pub_id在页面上一样)而不是pages0_.pub_id(在where子句中正确使用)。
这是在调用Publication.getPages()。isEmpty()
时发生的答案 0 :(得分:0)
也许,您可以向mappedBy
添加@OneToMany
并删除其@JoingTable
。与OneToMany / ManyToOne的双向关系双方都不需要@JoingTable
,您只需要在关系所有者处使用它。