gpt4 book ai didi

java - 使用 Hibernate - 仅获取指定字段(按名称)

转载 作者:行者123 更新时间:2023-11-30 11:23:00 25 4
gpt4 key购买 nike

假设我们有 2 个这样的类:

人:

@Entity
@Table (name= "person")
public class Person
{
@Id
@GeneratedValue
@Column(name = "Id")
private Long id;

@Column(name = "name")
private String name;

@Column(name = "phone")
private String phone;

@OneToMany(mappedBy = "bank")
private List<Bank> banks = new ArrayList<>();
}

银行:

@Entity
@Table(name = "bank")
public class Bank {

@Id
@GeneratedValue
@Column(name = "id")
private Long id;

@Column(name = "bank")
private String name;

@Columt(name = "phone")
private String phone;

@ManyToOne
@JoinColumn(name = "client_id")
@NotFound(action = NotFoundAction.EXCEPTION)
private Person person;
}

我们只想获取某些字段:person.name, person.phone, person.bank.phone

我试过这样做:

List result = session.createCriteria( Person.class )
.setProjection( Projections.projectionList()
.add( Projections.property( "name" ) )
.add( Projections.property( "phone" ) )
.add( Projections.property( "banks.phone" ) )
).list();

但最后我得到了这个异常(exception):

org.hibernate.QueryException: could not resolve property: banks.phone of: training.net5.Person
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:98)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:61)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1964)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:511)
at org.hibernate.criterion.PropertyProjection.toSqlString(PropertyProjection.java:67)
at org.hibernate.criterion.ProjectionList.toSqlString(ProjectionList.java:116)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getSelect(CriteriaQueryTranslator.java:379)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:110)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:92)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:97)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1651)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
at training.net5.MainClass.main(MainClass.java:82)
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:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
could not resolve property: banks.phone of: training.net5.Person

基本上需要的是一种通过相对于根实体的路径仅获取某些字段的方法。所以我想知道我是否可以为此使用投影,或者我应该寻找另一种方法。

附注对不起我的英语我不是母语人士

最佳答案

您可以尝试从另一个实体开始吗?

List result = session.createCriteria( Bank.class )
.setProjection( Projections.projectionList()
.add( Projections.property( "person.name" ) )
.add( Projections.property( "person.phone" ) )
.add( Projections.property( "phone" ) )
).list();

也许应该是

@OneToMany(mappedBy = "person")
private List<Bank> banks = new ArrayList<>();

关于java - 使用 Hibernate - 仅获取指定字段(按名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21451658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com