简体   繁体   English

使用JPA / Hibernate坚持Map <Long,Boolean>吗?

[英]Persist a Map< Long, Boolean > with JPA/Hibernate?

I am not an expert in JPA/Hibernate and I really do not know if what I am trying to achieve is not possible or I am doing it wrong and since I expect that is the latter here goes nothing. 我不是JPA / Hibernate的专家,我真的不知道我试图实现的目标是不可能的,还是我做错了,因为我希望后者在这里什么都没有做。

I have a Map< Long, Boolean > which I am trying to persist into a table and was following the example described here: Storing a Map<String,String> using JPA 我有一个Map <Long,Boolean>,我试图将其持久化到表中,并遵循此处描述的示例: 使用JPA存储Map <String,String>

And other examples which basically describe the same practice. 和其他基本描述相同做法的示例。

What I was trying to do is: 我试图做的是:

@Id    
private Long id;

@ElementCollection
@CollectionTable(name = "state_map", joinColumns = @JoinColumn(name = "id"))
@MapKeyColumn(name = "name")
@Column(name = "value")
@Type(type = "org.hibernate.type.TrueFalseType")
private Map<Long, Boolean> myBooleanMap;

And my table is defined as: 我的表定义为:

CREATE TABLE if not exists state_map
(id BIGINT NOT NULL,
name BIGINT NOT NULL,
value CHAR);

But then I get a HibernateException: Wrong column type in STATE_MAP for column value. 但是然后我得到一个HibernateException:STATE_MAP中列值的列类型错误。 Found: bigint, expected: char(255) 找到:bigint,预期:char(255)

When I change the bigint to char(255) the entityManager starts but when I am trying to put in the map and persist I am getting a java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Boolean. 当我将bigint更改为char(255)时,entityManager启动,但是当我尝试放入地图中并坚持执行时,我得到了java.lang.ClassCastException:java.lang.Long无法转换为java.lang.Boolean。 I suppose that the @Type annotation is applied on the key column instead on the value column. 我想@Type注释应用于键列而不是值列。

The only way I managed to make it work is with Map< String, String >. 我设法使其起作用的唯一方法是使用Map <String,String>。 Also I have tried to do everything in the same table which did not work so I have to declare another with just the surrogate id which is mapped to the entity: 我也尝试在同一张表中做所有无法正常工作的事情,因此我必须声明一个仅具有映射到实体的代理ID的表:

CREATE TABLE if not exists state_id
(id serial primary key);

@Entity
@Table(name = "state_id")
public class StateModel {...}

Hope somebody can help, thanks. 希望有人可以帮助,谢谢。

UPDATE - SOLUTION 更新-解决方案

I have solved by introducing an embeddable to wrap the boolean value column: 我已经解决了通过引入一个可包装布尔值列的可嵌入对象:

@Embeddable
class BooleanWrapper{

     @Column(name = "value")
     @Type(type = "org.hibernate.type.TrueFalseType")
     private Boolean myBoolean;}

And my map became: 我的地图变成:

@ElementCollection
@CollectionTable(name = "state_map", joinColumns = @JoinColumn(name = "id"))
@MapKeyColumn(name = "name")
private Map<Long, BooleanWrapper> myBooleanMap;

I see you have annotated myBooleanMap with @Type(type = "org.hibernate.type.TrueFalseType") which I think is incorrect. 我看到您用@Type(type =“ org.hibernate.type.TrueFalseType”)注释了myBooleanMap,我认为这是不正确的。 You can define TrueFalseType for a CHAR column not for map. 您可以为CHAR列而不是映射定义TrueFalseType。 I don't think bigint is a problem 我认为bigint不是问题

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

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