Spring hibernate - 多个实体之间的多对一关系

时间:2016-04-07 17:06:28

标签: java spring hibernate spring-mvc jpa

我创建了一个类调用Comment,它将与两个类建立关系:House和User。房子和用户entites已经在我的数据库中,并且房子已经与用户有一对多的关系,但它不能用于评论。每当我尝试重新加载我的服务器并且评论表没有保留时,我就会收到错误404。以下是我的代码:

用户:

@Entity
@Table(name = "users")
public class User {

    @NotBlank(groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    @Size(min = 8, max = 15, groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    @Pattern(regexp = "^\\w{8,}$", groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    @Id
    @Column(name = "username")
    private String username;

    @NotBlank(groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    @Pattern(regexp = "^\\S+$", groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    @Size(min = 8, max = 15, groups = { FormValidationGroup.class })
    private String password;

    @Email(groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    private String email;

    @NotBlank(groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    @Size(min = 8, max = 60, groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
    private String name;

    private boolean enabled = false;
    private String authority;

    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "id")
    private House house;

    public User() {
    }
}

我的评论:

@Entity
@Table(name = "comment")
public class Comment {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Lob
    @Column(name = "text")
    private String text;

    @Column(name = "date")
    private Date date;

    @ManyToOne(optional = false)
    @JoinColumn(name = "username", referencedColumnName = "username")
    private User user;

    @ManyToOne(optional = false)
    @JoinColumn(name = "id", referencedColumnName = "id")
    private House house;

    public Comment() {
    }
}

有人能说出我做错了吗?

0 个答案:

没有答案