Getting fewer columns with hibernate
Asked Answered
D

3

8

I have a table with 11 columns, but I need to get only 2 of them in my application, I'm using spring/hibernate/DAO combination. For now I have a domain class which includes all 11 fields, and mapping file which maps all 11 columns in table. How do I use get just 2 of them not all?

Duran answered 1/4, 2010 at 13:25 Comment(0)
M
15

Either:

  1. Use projections - Pro: nothing to add - Con: Not typesafe (the result is a List of rows where each row is anObject[]):

     select f.foo, f.bar from FatEntity f
    
  2. Use a constructor expression in the SELECT clause (the specified class is not required to be an entity or to be mapped to the database) - Pro: typesafe solution - Con: More classes, unless you reuse FatEntity as holder in which case many fields will be null:

     select new com.acme.FatEntityDetails(f.id, f.foo, f.bar) from FatEntity f
    

    Note that if an entity class name is specified in the SELECT NEW clause, the resulting entity instances are in the new state (no persistent identity).

  3. Use another entity mapped on the same table with only the required fields - Pro: It's a real entity that you can modify and update - Con: More classes.

     from LightEntity
    

The main differences between #2 and #3 are:

  • 2 doesn't require the holder to be an entity at all.

  • the holder in #2 could be an entity mapped on another table.
  • if #2 returns entities, they are in a new state (this might be a problem, or not).
Mcdonough answered 1/4, 2010 at 14:23 Comment(5)
@Pascal Thivent can't I just leave 2 fields in my domain class and mapping file?Duran
@Gandalf Well, I thought that someone was using the other fields. If this is not true, then you can indeed change your current entity.Mcdonough
Thank you for the multiple solutions, and explanatios, how do you come up with that anyways? expirience? reading books, training? I'll accept your answer. if you could point me to the right direction it would be great, like: websites, examples, books.Duran
@Gandalf Mostly experience (I'm working with Hibernate since 2003, it helps) and reading documentation, books, specifications. In this particular case, I would recommend "Hibernate in Action" and the Hibernate documentation. The JPA specification (jcp.org/en/jsr/detail?id=317) is also a good reading.Mcdonough
@GandalfStormCrow There is also a 4th option: Move the column to a separate table and join with @OneToOne(fetch = FetchType.LAZY). Then if you do want to load the column just add a join fetch to the query: https://mcmap.net/q/1322158/-spring-data-jpa-how-to-avoid-loading-a-columnMeander
C
8

Try:

SELECT myEntity.one, myEntity.two FROM MyEntity myEntity

You can even do :

SELECT new MyEntityDescription(myEntity.one, myEntity.two) FROM MyEntity myEntity

to get a list of entity descriptions.

Cene answered 1/4, 2010 at 13:27 Comment(0)
E
3

If you never need more than those 2 columns of the table, you could change your hibernate mapping to map only those 2 needed columns to the entity class. Only map those table columns you want to access in your application. Keep in mind, that database constraints on the "ignored" columns can be violated like not null constraints, foreign keys or unique constraints.

Emmieemmit answered 1/4, 2010 at 13:36 Comment(1)
what about domain class do I need to have 2 fields in there as well ?Duran

© 2022 - 2024 — McMap. All rights reserved.