查询Hibernate实体时出错

时间:2013-12-26 13:17:45

标签: java hibernate exception

我有两个Hibernate实体,如下所示,它们之间有关系,我在查询它们时遇到了问题。

@Entity
@Table(name="tdbs", catalog="dbcreator")
public class DB extends DBEntity {

    @Id
    @Column(name="id", unique=true, nullable=false)
    @GenericGenerator(name="gen",strategy="increment")
    @GeneratedValue(generator="gen")
    private Integer id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="usr", referencedColumnName="usr", nullable=false)
    private User user;


@Entity
@Table(name="tusers",catalog="dbcreator", uniqueConstraints = @UniqueConstraint(columnNames="email"))
@XmlRootElement(name = "User")
public class User extends DBEntity {

    @Id 
    @Column(name="usr", unique=true, nullable=false, length=30)
    private String usr;

我想要查询的是获取链接到特定用户的数据库实体列表("用户"变量包含实际用户名)。我使用以下查询:

List<DB> list = sessionFactory.getCurrentSession().createQuery(
                "from DB p where p.user=:usrId").setString("usrId", user).list();

然而,这给了我一些奇怪的错误,如下所示。任何人都知道如何对其进行排序?

java.lang.ClassCastException: com.dbcreator.persistence.hibernate.model.User_$$_javassist_5 cannot be cast to javassist.util.proxy.Proxy
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:148)
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:73)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:758)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4419)
    at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:333)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:259)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1017)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:944)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:671)
    at org.hibernate.type.EntityType.resolve(EntityType.java:489)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1112)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:969)
    at org.hibernate.loader.Loader.doQuery(Loader.java:917)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348)
    at org.hibernate.loader.Loader.doList(Loader.java:2548)
    at org.hibernate.loader.Loader.doList(Loader.java:2534)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2364)
    at org.hibernate.loader.Loader.list(Loader.java:2359)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1194)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at com.dbcreator.persistence.impl.DatabaseDAO.getDBList(DatabaseDAO.java:30)

1 个答案:

答案 0 :(得分:2)

应该如下:

sessionFactory.getCurrentSession().
createQuery("from DB p where p.user.usr=:usrId").setParameter("usrId", user)

<强>旁注

一致且有意义的命名将提高可读性。我个人的选择应该是:

sessionFactory.getCurrentSession().
createQuery("from DB db where db.user.usr=:usr").setParameter("usr", userString)

也许User.usr成员变量应该被称为User.idUser.name,具体取决于它实际代表的内容。