简体   繁体   中英

hibernate h2 not generating relations

I'm trying to generate Hibernate mapping to my H2 database. I have 2 tables for test, called users and users_groups. They look like:

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

users_groups
    user_group_id     integer     PK
    name              varchar

And the problem is that hibernate generate entities like that:

@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;
}

So no relation annotations are generated, like @OneToMany or @ManyToMany etc. What am I doing wrong? Thanks for your help. ps I want it to generate mapping like Users class with field of UserGroup type

If the classes were auto generated like this check your relation in the database between the two tables and make sure you choose the right schema your mapping is completely wrong the for example :-

1-the auto generated classes your mapping are missing some columns, class User doesn't contain password and login columns and class UsersGroups doesn't contain name column.

2- class User doesn't have @table annotation

They should look something like this :-

Class 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;
   }
   }

Class 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;
        }

    }

Check this full example for one to many mapping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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