hibernate h2没有产生关系

时间:2015-10-10 11:12:08

标签: java hibernate orm mapping h2

我正在尝试为我的H2数据库生成Hibernate映射。 我有2个表用于测试,称为users和users_groups。 他们看起来像:

users table:
    user_id           integer     PK
    login             varchar
    password          varchar
    user_group_id     integer     FK

users_groups
    user_group_id     integer     PK
    name              varchar

问题是hibernate会生成这样的实体:

@Entity
public class Users {
private int userId;
private int userGroupId;

@Id
@Column(name = "USER_ID", nullable = false)
public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}


@Basic
@Column(name = "USER_GROUP_ID", nullable = false)
public int getUserGroupId() {
    return userGroupId;
}

public void setUserGroupId(int userGroupId) {
    this.userGroupId = userGroupId;
}

@Entity
@Table(name = "USERS_GROUPS", schema = "PUBLIC", catalog = "DATABASE")
public class UsersGroups {
private int userGroupId;

@Id
@Column(name = "USER_GROUP_ID", nullable = false)
public int getUserGroupId() {
    return userGroupId;
}

public void setUserGroupId(int userGroupId) {
    this.userGroupId = userGroupId;
}

所以没有生成任何关系注释,比如@OneToMany或@ManyToMany等。我做错了什么?谢谢你的帮助。 附:我想要它生成映射 具有UserGroup类型字段的用户类

1 个答案:

答案 0 :(得分:0)

如果这些类是自动生成的,请检查两个表之间数据库中的关系,并确保选择正确的模式,例如: -

1 - 自动生成的类,您的映射缺少某些列,class User不包含密码登录列,而class UsersGroups不包含不包含名称列。

2- class User没有@table注释

他们应该看起来像这样: -

类UserGroups

@Entity
@Table(name = "USERS_GROUPS", schema = "PUBLIC", catalog = "DATABASE")
public class UsersGroups implements java.io.Serializable {
   private int userGroupId;
   private String name;
   private Set<Users> users = new HashSet<Users>(0);

   public UsersGroups() {
    }

   @Id
   @GeneratedValue(strategy = IDENTITY) //this to make the id auto increment
   @Column(name = "user_group_id", nullable = false)
   public int getUserGroupId() {
       return userGroupId;
   }

   public void setUserGroupId(int userGroupId) {
       this.userGroupId = userGroupId;
   }

   // if name column is not unique / nullable remove values from annotation 
   @Column(name = "name", unique = true, nullable = false, length = 10)
    public String getName() {
       return this.name;
    }
    public void setName(String name) {
    this.name= name;
    }
   @OneToMany(fetch = FetchType.EAGER, mappedBy = "users_groups")
   public Set<Users> getUsers() {
       return this.users;
   }

   public void setUsers(Set<Users> users) {
       this.users= users;
   }
   }

班级用户

@Entity
@Table(name = "users", schema ="PUBLIC" , catalog ="DATABASE")
public class Users implements java.io.Serializable {

    private Integer userId;
    private UsersGroups usersGroups;
    private String password;
    private String login;

    public Users() {
    }


    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "user_id", unique = true, nullable = false)
    public Integer getUserId() {
        return this.userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "user_group_id", nullable = false)
        public UsersGroups getUsersGroups() {
            return this.usersGroups;
        }

        public void setUsersGroups(UsersGroups usersGroups) {
            this.usersGroups = usersGroups;
        }

        @Column(name = "password",length = 10)
        public String getPassword() {
            return this.password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Column(name = "login",length = 10)
        public String getLogin() {
            return this.login;
        }

        public void setLogin(String login) {
            this.login = login;
        }

    }

检查此完整example是否有一对多的映射