如何使用hibernate在POJO上正确映射Set <entity>?

时间:2016-03-05 22:36:20

标签: java hibernate set persistence

我正试图将Uncaught Exception: Error: Cannot find module 'electron' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:286:25) at Module.require (module.js:365:17) at require (module.js:384:17) at Object.<anonymous> (/Users/fabuy/lab/tilo-desktop/releases/tilo-desktop-darwin-x64/tilo-desktop.app/Contents/Resources/app/main.js:3:18) at Module._compile (module.js:434:26) at Object.Module._extensions..js (module.js:452:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Object.<anonymous> (/Users/fabuy/lab/tilo-desktop/releases/tilo-desktop-darwin-x64/tilo-desktop.app/Contents/Resources/atom.asar/browser/lib/init.js:123:10) 类与User绑定,以存储每个用户的个人资料。我摆弄了一下,现在有点失落。我Set类的设定部分目前定义为:

User

我知道我在Set中使用了不同的类型。那是因为@ElementCollection(targetClass = UserProfile.class) @JoinTable(name = "HRM_USER_USER_PROFILE", joinColumns = { @JoinColumn(name = "id_user") }, inverseJoinColumns = { @JoinColumn(name = "id_profile") }) @Column(name = "id_profile") @Cascade(org.hibernate.annotations.CascadeType.ALL) private Set<String> userProfiles = new HashSet<String>(0); 被定义为:

UserProfile

其中UserProfileType是枚举声明为:

@Entity
@Table(name="HRM_USER_PROFILE")
public class UserProfile extends BasePojo {

    @Enumerated(EnumType.STRING)
    @Column(name="type", length=15, unique=true, nullable=false)
    private UserProfileType type;

    public UserProfileType getType() {
        return type;
    }

    public void setType(UserProfileType type) {
        this.type = type;
    }

我知道这与注释public enum UserProfileType { USER("USER"), DBA("DBA"), ADMIN("ADMIN"); String userProfileType; private UserProfileType(String userProfileType){ this.userProfileType = userProfileType; } public String getUserProfileType(){ return userProfileType; } 有关。但我很困惑。

我在哪里错了?提前谢谢!

1 个答案:

答案 0 :(得分:0)

  

试试这个&gt;&gt;&gt;

@Enumerated(EnumType.STRING) //enum type mapping with String Values
@Column(name="type", length=15, unique=true, nullable=false)
private UserProfileType type;

如果type为Numerical Values

,您还可以使用ordinal
@Enumerated(EnumType.ORDINAL) //enum type mapping with ordinal Values
@Column(name="type", length=15, unique=true, nullable=false)
private UserProfileType type;