问题描述
我有这个课程,我正试图从用户表中访问 mobileNumbers
和 roles
,但是我习惯了 lazy
出现'惰性初始化错误
.
I have this class , and i'am trying to acess mobileNumbers
and roles
from the user table , but being lazy
intialized i keep getting 'lazy intialization error
.
我不希望删除 LAZY
提取,因为很少需要完全访问该对象,但这是必需的.因此,为了克服这一点,我尝试添加 @Transactional
,但在本文中 https://codete.com/blog/5-common-spring-transactional-pitfalls/在我看来,这是一个不好的选择,我尝试使用 join fetch
,但它一直提供 multiplebagfetchexception
,因此我试图一次一次获取它们(我的意思是Element Collections)
I do not wan't to remove the LAZY
fetch as complete access of the object is rarely required , but is required. So to overcome it first i tried to add @Transactional
but going by this article https://codete.com/blog/5-common-spring-transactional-pitfalls/ it seems a bad approch in my case, i tried using join fetch
but it keeps giving multiplebagfetchexception
,hence i tried to fetch them one at a time ( Element Collections i mean)
使用此存储库类
public interface UserRespository extends JpaRepository<UserDao, Long> {
Optional<UserDao> getByUserNameIgnoreCase(String userName);
// Optional<UserDao> findByUserNameIgnoreCase(String userName);
@Query(value = "select dao.roles.roles from UserDao dao inner join dao.roles r on dao.userName in elements(r.userName) and upper(dao.userName) = upper(?1)")
Object getByUserNameIgnoreCaseComplete2(String userName);
}
然后我得到了他的错误
Caused by: org.hibernate.QueryException: cannot dereference scalar collection element: roles
at org.hibernate.persister.collection.ElementPropertyMapping.toType(ElementPropertyMapping.java:33) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.persister.collection.AbstractCollectionPersister.toType(AbstractCollectionPersister.java:1644) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:396) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:515) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:682) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:265) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:205) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:109) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:104) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.tree.DotNode.resolveSelectExpression(DotNode.java:744) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.HqlSqlWalker.resolveSelectExpression(HqlSqlWalker.java:1057) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2295) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2232) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1503) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:585) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:271) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
... 112 common frames omitted
我不确定为什么会一直出现此错误.还有其他方法可以获取整个对象吗?
I'm not sure why i keep getting this error.Is there any other way i can get the entire object??
请帮助.
实际班级
@Entity
@Table(name = "users")
@Data
@NoArgsConstructor
public class UserDao implements Serializable {
@Id
@GeneratedValue
private long id;
@Column(name = "user_name", nullable = false, unique = true)
private String userName;
@Column(name = "password", nullable = false)
private String password;
@ElementCollection(targetClass = java.lang.String.class)
@CollectionTable(name = "mobile_numbers_List",joinColumns = @JoinColumn(name = "user_name",referencedColumnName = "user_name"))
@Column(name = "mobile_number")
private List<String> mobileNumbers;
@ElementCollection(targetClass = java.lang.String.class)
@CollectionTable(name = "roles_list",joinColumns = @JoinColumn(name = "user_name",referencedColumnName = "user_name"))
@Column(name = "roles")
private List<String> roles;
}
推荐答案
您,并且上一个问题和该问题的答案相同.
This seems like a similar question you previous questio , and the answer to the previous one and this one is same.
您的查询在这里错误
@Query(value = "select dao.roles.roles from UserDao dao inner join dao.roles r on dao.userName in elements(r.userName) and upper(dao.userName) = upper(?1)")
Object getByUserNameIgnoreCaseComplete2(String userName);
它正在尝试获取 dao.roles.roles
,如果它甚至不存在.
it's trying to fetch dao.roles.roles
which if does not even exist.
这是您应该尝试的一些更改
here are some changes you should try
- 将
dao.roles.roles
更改为r(因为您已经创建了别名,让我们使用它) - 删除元素(r.userName)中的
dao.userName
不是必需的,spring会自动执行此操作,甚至无需指定它. - 将返回类型更改为list,它是
ElementCollection
,即元素的 Collection
- change
dao.roles.roles
to r (as you already created an alias , let's use it) - remove
dao.userName in elements(r.userName)
it's not required, spring does this automatically without even specifying it. - change the return type to list , it's
ElementCollection
i.e Collection of elements
这是您最终查询的样子
@Query(value = "select r from UserDao dao inner join dao.roles r on upper(dao.userName) = upper(?1)")
List<String> getByUserNameIgnoreCaseComplete2(String userName);
这篇关于引起原因:org.hibernate.QueryException:无法取消引用标量集合元素:角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!