Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39997

Re: Hibernate get fields

From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Hibernate get fields
Date 2023-08-23 19:43 +0200
Organization A noiseless patient Spider
Message-ID <uc5gfj$3153r$1@dont-email.me> (permalink)
References <0d218a53-9293-4203-9d58-598e898d2c87n@googlegroups.com>

Show all headers | View raw


On 23/08/2023 16:15, e.d.pro...@gmail.com wrote:
> Currently using Hibernate 5.6.15.Final as we're currently stuck with Java 8.
> I have a table with say 50 fields.
> I have a DTO with 50 fields.
> So the DTO looks something like this.
> @Table
> public class MyDTO implements Serializable {
>      @Id
>      @Column(name = "FIELD_ONE")
>      private String fieldOne;
>      @Column(name = "FIELD_TWO")
>      private String fieldTwo;
> ...
> }
> 
> Now I write a query in my DAO to get just those 2 fields from all records.
> This works. Is there a cleaner way without creating a new file containing an interface to reference just those 2 fields?
>    private static final String MY_QUERY = "SELECT fieldOne, fieldTwo FROM MyDTO";
>    try (Session session = MyHibernateUtil.getSessionFactory().openSession();) {
>      Query<? extends Object[]> query = session.createQuery(MY_QUERY,(new Object[]{}).getClass());
>      List<? extends Object[]> results = query.list();
>      final Map<String,String> resultMap = new HashMap<>(100);
>      results.stream().forEach(result -> resultMap.put(String.valueOf(((Object[])result)[0]), String.valueOf(((Object[])result)[1])));
> }

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.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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