Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39998
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2023-08-24 06:11 -0700 |
| References | <0d218a53-9293-4203-9d58-598e898d2c87n@googlegroups.com> <uc5gfj$3153r$1@dont-email.me> |
| Message-ID | <532f9463-1310-4f8f-8ba3-6b16a5cbcd23n@googlegroups.com> (permalink) |
| Subject | Re: Hibernate get fields |
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
On Wednesday, August 23, 2023 at 1:43:28 PM UTC-4, Daniele Futtorovic wrote:
> You can probably make the DTO an inner class, yeah. No new class file
> that way.
>
> But really, is this something you should be worrying about?
>
> --
> DF.
The only way I've found to select 2 fields from a table that has more than 2 fields is to create a 2 field constructor, which doesn't work if you have 2 queries with the same number and type of fields but different fields, or create a static inner class which requires duplicating the field definitions and fully qualifying the class.
@Entity
@Table(name = "V_TABLE_DEF", schema = "MYSCHEMA")
@Getter
@Setter
public class MyDTO implements Serializable {
@Id
@Column(name = "FIELD_ONE")
private String fieldOne;
@Column(name = "FIELD_TWO")
private String fieldTwo;
public MyDTO(){}
public MyDTO(String f1, String f2) {setFieldOne(f1);setFieldTwo(f2);}
@Getter
@Setter
public static class F1F2 {
public F1F2(String f1,String f2) {setFieldOne(f1);setFieldTwo(f2);}
@Id
@Column(name = "FIELD_ONE")
private String fieldOne;
@Column(name = "FIELD_TWO")
private String fieldTwo;
}
... // more fields
}
private static final String MY_QUERY = "SELECT new MyDTO(fieldOne, fieldTwo) FROM MyDTO";
private static final String MY_INNER_QUERY = "SELECT new org.mypackage.MyDTO$F1F2(fieldOne, fieldTwo) FROM MyDTO";
try (Session session = MyHibernateUtil.getSessionFactory().openSession();) {
Query<MyDTO> query = session.createQuery(MY_QUERY,MyDTO.class);
List<MyDTO> results = query.list();
Query<MyDTO.F1F2> innerQuery = session.createQuery(MY_INNER_QUERY,MyDTO.F1F2.class);
List<MyDTO.F1F2> innerResults = innerQuery.list();
}
If we want to be able to use the outer class, perhaps setting some other values and passing it back to a Hibernate update method, we can create a constructor for the outer class that accepts an instance of the inner class and convert the results after the fact (not in the Hibernate select).
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Hibernate get fields "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-08-23 07:15 -0700
Re: Hibernate get fields Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2023-08-23 19:43 +0200
Re: Hibernate get fields "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-08-24 06:11 -0700
Re: Hibernate get fields Arne Vajhøj <arne@vajhoej.dk> - 2023-08-25 19:58 -0400
csiph-web