Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How to map Map<enum, Boolean> with JPA and Hibernate
PostPosted: Tue Feb 14, 2017 5:23 pm 
Newbie

Joined: Wed Nov 16, 2016 8:40 am
Posts: 2
I have following two classes:

Code:
@Entity(name = "user_table")
public class User {

    @Id
    @Column(name = "id")
    @GeneratedValue
    Long id;

    @Column(name = "login",nullable = false,unique = true)
    private String login;

    private Map<UserSettingName, Boolean> settingMap = new HashMap<>();
   
   //other fields
}

public enum UserSettingName {
    /*result table specific settings*/
    RESULT_TABLE_SHOW_DNS,
    RESULT_TABLE_SHOW_SYS_NAME,
    RESULT_TABLE_SHOW_IP_ADDRESS,
    RESULT_TABLE_SHOW_UPTIME,
    RESULT_TABLE_SHOW_DESCRITPION,
    RESULT_TABLE_SHOW_SYS_LOCATION
    ;
}


what annotations should i use on field settingMap. I tried
Code:
@OneToMany( cascade = CascadeType.ALL, orphanRemoval = true)
@MapKeyEnumerated(EnumType.STRING)

but have an error:
nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.slavick.macmonitor.model.user.User.settingMap[java.lang.Boolean]


Top
 Profile  
 
 Post subject: Re: How to map Map<enum, Boolean>
PostPosted: Wed Feb 15, 2017 2:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 976
You can't use @OneToMany since this requires a child Entity relationship, and in your case, you have a basic elements collection instead.

Therefore, your mapping should look like this:

Code:
@ElementCollection
@MapKeyEnumerated(EnumType.STRING)
private Map<UserSettingName, Boolean> settingMap = new HashMap<>();


Check out this article if you're interested in optimizing the performance of unidirectional collections.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.