6

I'm making a database migration tool and am dealing with a source database that's very unwieldy. It's basically one giant table that has upwards of 40-50 columns. However not all of these columns are useful to me. I only want maybe a dozen or so of them. Once I get that data I'm making requests to a web service that will handle everything on the destination end of migration.

My options are basically creating queries manually to only select the columns I want, or make an Entity that maps the columns I want. I'm not really that familiar with using JPA so I'm not sure if this is possible or ok.

Can I do something like

@Entity
class SomeEntity{

    @Column(name = "ColumnA")
    private String columnA;
    @Column(name = "ColumnB")
    private String columnB;
}

if the columns in the database are, for example

Column A | Column B | Column C | Column D

Will EclipseLink map only the columns I annotate or will it complain trying to map columns in the db to fields that don't exist in my Entity? I know @Transient will mark fields that should not be persisted. But I want to do the opposite, and ignore database columns and only partially map the table to a class.

JMD83
  • 105
  • 1
  • 10
  • 2
    If they are read-only, you may map only several columns to entities, no problem with that. Fetched will be only those columns that you declared in `@Entity`. To be safe, make setters `protected` to enforce immutability at application level. – Alex Salauyou Mar 23 '16 at 18:24
  • Basically yes, but you have to define an `@Id` (or equivalent) in any case. There is no need to create a view on database level, like it is suggested in the accepted answer. – stg Mar 24 '16 at 06:48

1 Answers1

1

you should create a view. Have the view hold as many colums as you deem relevant. Define those columns as fields in a class and wire those with hibernate annotations as usual. Be aware though, that you can only perform selects on a view, and so inserts / updates / deletes are out of the question.

  • The massive table is read only so only performing selects is fine. Can you maybe elaborate on what a view is? What would happen in my above example? – JMD83 Mar 23 '16 at 18:20
  • 1
    @JMD83 wiews are created at DB level, you really don't need them unless some tables must be joined. – Alex Salauyou Mar 23 '16 at 18:28