简体   繁体   English

在Hibernate中映射Java 8个月枚举列表

[英]Mapping a list of Java 8 month enum in Hibernate

is it possible to map a list of java.time.Month to a seperate Table like so: 是否可以像这样将java.time.Month列表映射到单独的Table:

|MyEntity             |    |MyEntity_Month    |    |Month          |
|---------------------|    |------------------|    |---------------|
|id : long            |    |myEntity_Id : long|    |id : long      |
|                     |----|month_Id : long   |----|               |
|---------------------|    |------------------|    |---------------|

I have tried it like this but it that only creates a table MyEntity_Month with a myEntity_Id and values: 我已经尝试过了,但是它只能创建一个带有myEntity_Id和值的表MyEntity_Month:

@ElementCollection(targetClass = Month.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "MyEntity_Months"
        , joinColumns = @JoinColumn(name = "myEntity_id"))
private List<Month> months;

The problem with that solution is that the values are repeated for each MyEntity instead of associated. 该解决方案的问题在于,对每个MyEntity重复这些值,而不是将其关联。 Database view of the above annotation configuration: 以上注释配置的数据库视图:

|MyEntity             |    |MyEntity_Month         | 
|---------------------|    |-----------------------|
|id : long            |    |myEntity_id | MONTH    |
|...                  |----|-----------------------|
|---------------------|    |1           | NOVEMBER |
                           |1           | DECEMBER |
                           |2           | JANUARY  |
                           |2           | DECEMBER |
                           |-----------------------|

Is it possible to map via assocation table so that I need only 12 Month Strings in the database? 是否可以通过关联表进行映射,以便数据库中仅需要12个月的字符串? I'm using Hibernate 4.3 / JPA 2.1 btw 我正在使用Hibernate 4.3 / JPA 2.1 btw

The type java.time.Month does not have @Entity annotations, so this is not recognized as an entity by hibernate. 类型java.time.Month没有@Entity批注,因此休眠不将其识别为实体。 You can achieve your desired table mapping if you introduce something like a MonthWrapper: 如果引入类似MonthWrapper的内容,则可以实现所需的表映射:

@Entity
public class MyEntity {

    @Id
    private Long id;

    @OneToMany(fetch = FetchType.EAGER)
    private List<MonthWrapper> months;
}


@Entity
public class MonthWrapper {

    @Id
    private Long id;

    @Enumerated(EnumType.STRING)
    private Month month;
}

Then if you run this through hibernate with a create-drop hbm2ddl.auto property, then you get the following tables: 然后,如果您使用create-drop hbm2ddl.auto属性在休眠状态下运行此hbm2ddl.auto ,则会得到下表:

Hibernate: create table MonthWrapper (id bigint not null, month varchar(255), primary key (id)) 休眠:创建表MonthWrapper(id bigint不为null,month varchar(255),主键(id))

Hibernate: create table MyEntity (id bigint not null, primary key (id)) 休眠:创建表MyEntity(ID bigint不为null,主键(id))

Hibernate: create table MyEntity_MonthWrapper (MyEntity_id bigint not null, months_id bigint not null) 休眠:创建表MyEntity_MonthWrapper(MyEntity_id bigint不为空,months_id bigint不为空)

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

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