问题
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.
回答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.
来源:https://stackoverflow.com/questions/36185667/can-i-have-an-entity-that-only-maps-to-some-columns-of-a-table