hibernate 4.1.7.Final具有未知的表id按顺序排列

时间:2012-09-17 14:09:24

标签: java hibernate postgresql jpa-2.0 jboss7.x

我正在尝试从4.0.1升级到hibernate 4.1.7.Final,我收到以下错误:

Failed to start service jboss.persistenceunit."****/********": org.jboss.msc.service.StartException in service jboss.persistenceunit."*****": Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_35]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_35]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_35] 

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: myatrium_intranet] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
... 3 more
Caused by: org.hibernate.HibernateException: Could not locate table which owns column [id] referenced in order-by mapping
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.determineTableNumberForColumn(JoinedSubclassEntityPersister.java:854)
at org.hibernate.persister.entity.AbstractEntityPersister.getTableAliasForColumn(AbstractEntityPersister.java:4865)
at org.hibernate.persister.collection.AbstractCollectionPersister$StandardOrderByAliasResolver.resolveTableAlias(AbstractCollectionPersister.java:1930)
at org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator$StandardOrderByTranslationImpl.injectAliases(OrderByFragmentTranslator.java:106)
at org.hibernate.persister.collection.AbstractCollectionPersister.getSQLOrderByString(AbstractCollectionPersister.java:751)
at org.hibernate.loader.JoinWalker.orderBy(JoinWalker.java:910)
at org.hibernate.loader.JoinWalker.orderBy(JoinWalker.java:822)
at org.hibernate.loader.AbstractEntityJoinWalker.initStatementString(AbstractEntityJoinWalker.java:123)
at org.hibernate.loader.AbstractEntityJoinWalker.initStatementString(AbstractEntityJoinWalker.java:108)
at org.hibernate.loader.AbstractEntityJoinWalker.initAll(AbstractEntityJoinWalker.java:90)
at org.hibernate.loader.AbstractEntityJoinWalker.initAll(AbstractEntityJoinWalker.java:77)
at org.hibernate.loader.entity.CascadeEntityJoinWalker.<init>(CascadeEntityJoinWalker.java:51)
at org.hibernate.loader.entity.CascadeEntityLoader.<init>(CascadeEntityLoader.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3785)
at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3722)
at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:1018)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:457)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
... 9 more

我不知道hibernate正在讨论哪个表。有什么方法可以了解它吗?我还需要添加任何特殊的东西才能让它发挥作用吗?请注意,我的数据结构适用于4.0.1。

任何帮助都会非常感激。

更新:我打开了hibernate的调试,我想知道如何找出导致问题的类。有什么方法可以知道它来自哪个类?只是提醒您,错误来自于在启动服务器之前hibernate执行的预加载查询。

提前致谢

1 个答案:

答案 0 :(得分:3)

我已经找到了最终会发生这种情况的情况,并且很快将提交一份关于hibernate的错误报告,并在我这样做时将其链接到此处。如果您有一个使用JOINED的继承策略的实体,并且您将父实体作为另一个实体中的集合引用并向该集合添加一个order by,则抛出OP发布的错误。

例如:

@Entity
@Table(name = "Joined_Parent")
@Inheritance(strategy = InheritanceType.JOINED)
public class JoinedParent
{
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;

    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "other_entity_id", nullable = false)
    private OtherEntity otherEntity;
}

@Entity
@Table(name = "Joined_Child")
@PrimaryKeyJoinColumn(name = "id")
public class JoinedChild
    extends JoinedParent
{
}

@Entity
@Table(name = "Other_Entity")
public class OtherEntity
{
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "otherEntity", orphanRemoval = true)
    @OrderBy("id ASC") // <-- this causes the error
    private List<JoinedParent> items = new ArrayList<JoinedParent>();
}

我不确定这是否仅在您将父级引用为集合或者是否与子实体一起发生时才会发生。我也不确定双向关系是否与它有任何关系。我在此处提交了错误报告https://hibernate.onjira.com/browse/HHH-7630