Hibernate:无法映射聚合/连接

时间:2015-04-20 11:20:10

标签: hibernate

我想将我的Native Sql查询结果映射到User实体,但是得到了Column' role_id'找不到错误。

nativeSql= "select u.* from users u JOIN user_funds f on  u.id = f.iduser where f.idfund = 15" 

如何加载聚合角色?

用户类的相关部分:

@Embeddable
 @Entity
 @Table(name="users")
public class User {

private Role role;


@OneToOne(cascade=CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinTable(name="user_roles",
    joinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")},
    inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}
)
public Role getRole() {
    return role;
}
}

错误日志。

StackTrace:
        Caused by: java.sql.SQLException: Column 'role_id' not found.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
        at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
        at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2815)
        at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:66)
        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:65)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247)
        at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:176)
        at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2903)
        at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1668)
        at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1600)
        at org.hibernate.loader.Loader.getRow(Loader.java:1500)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:712)
        at org.hibernate.loader.Loader.processResultSet(Loader.java:940)
        at org.hibernate.loader.Loader.doQuery(Loader.java:910)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
        at org.hibernate.loader.Loader.doList(Loader.java:2516)

0 个答案:

没有答案