Question

Let's say we have 2 classes like these:

person:

@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<>();
}

bank:

@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;
}

And we want to get only certain fields: person.name, person.phone, person.bank.phone

I have tried to do something like this:

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

But in the end I have got this 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

Basically what is needed is a way to get only certain fields by their path relative to root entity. So I wonder if I can use projection for this or I should look for another approach.

p.s. sorry for my english I am not a native speaker

Was it helpful?

Solution

Can you try starting at the other entity?

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

And maybe it should be

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

OTHER TIPS

What you are trying to get is called fetch groups. If you have a table with many columns, may be you need to normalize it, rather then using this technique. In your code banks has OneToMany relation. What are you expecting?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top