This is possible with constructor expressions in your query. Normally you would use this with a custom DTO, but it should work with the entity too. First create an additional constructor in your entity taking only the needed fields:
public Address() {
//default constructor
}
public Address(String city) {
this.city = city;
}
Your query could then look like this:
select new your.package.Address(a.city) from Address a where ...
Note that the resulting object is not attached to the EntityManager, so changes to it won't be automatically persisted.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…