Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39997
| Path | csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Hibernate get fields |
| Date | Wed, 23 Aug 2023 19:43:14 +0200 |
| Organization | A noiseless patient Spider |
| Lines | 32 |
| Message-ID | <uc5gfj$3153r$1@dont-email.me> (permalink) |
| References | <0d218a53-9293-4203-9d58-598e898d2c87n@googlegroups.com> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Wed, 23 Aug 2023 17:43:15 -0000 (UTC) |
| Injection-Info | dont-email.me; posting-host="4f584cd0bf0352194bd0d51548174da7"; logging-data="3183739"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qv1vVKGtQWiBHHZl69uVF" |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 |
| Cancel-Lock | sha1:vn2p2UA05Waq5BgD75OMJVU4O5g= |
| Content-Language | en-US |
| In-Reply-To | <0d218a53-9293-4203-9d58-598e898d2c87n@googlegroups.com> |
| Xref | csiph.com comp.lang.java.programmer:39997 |
Show key headers only | 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
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