我正在使用带有 Spring 的 Hibernate 4.1.7 Java,在 MySql 上工作。
我有一个 Publication 类,它是抽象的,有 3 个子类,还有一个 Page 类,它也是抽象的,有 2 个子类。
在 Publication 中有一个 List 并且在 Page 我有一个 Publication,所以这是一个常见的 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,它就会生成一个查询,在 Page 子类表上搜索 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 在 Page 上)而不是 pages0_.pub_id(在 where 子句中正确使用)。
这发生在调用 Publication.getPages().isEmpty()