简体   繁体   English

如何将带有一长串通用列的表映射到JPA实体

[英]How to Map a table with a long list of generic columns to a JPA entity

Consider this use case : a mysql table X and a java JPA entiy X. 考虑以下用例:mysql表X和java JPA entiyX。

create table X 
(id int(11),
 name varchar(200),
 value1 bigint(16),
 value2 bigint(16),
 value3 bigint(16),
 .
 .
 value100 bigint(16));

I would like to map to an entity like this : 我想映射到这样的实体:

@Entity("x")
@Table("x")
public class X {

@Id
@Column("id)
public int getId ...

@Column("name")
public String getName ..

@SomeMagicalExpression
public List<Long> getValues()..
}

I am well aware of the option to create a join table where each value is a row, I am trying to get away from performing joins. 我很清楚创建连接表的选项,其中每个值都是一行,我试图摆脱执行连接的麻烦。 I would really hate to create a 100 getters and setters. 我真的很讨厌创建100个getter和setter。

Bit of a shot in the dark, but maybe the OpenJPA @Columns annotation will suit your needs.. 有点困难,但也许OpenJPA @Columns注释将适合您的需求。

http://docs.oracle.com/html/E24396_01/ref_guide_mapping_custom.html#ref_guide_mapping_custom_field_conf_extex http://docs.oracle.com/html/E24396_01/ref_guide_mapping_custom.html#ref_guide_mapping_custom_field_conf_extex

Not sure if you could use it to map multiple columns to a collection though.. Maybe subclass ArrayList and override its constructor to take in a number of values to populate the list. 不确定是否可以使用它将多个列映射到一个集合。也许是ArrayList的子类,并重写其构造函数以采用许多值来填充列表。 (If my assumption that the values used in @Columns must correspond to a constructor is correct..) (如果我认为@Columns中使用的值必须对应于构造函数的假设是正确的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM