我已尝试制作此实体:
@Entity
@Table(name = "rate")
public class Rate {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "rate_id")
private Long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User answerer;
@ManyToOne
@JoinColumn(name = "answer_id")
private Answer answer;
@Column(name = "rate_rate")
private Integer rate = AnswerConstants.MAX_RATE;
//pluss getter-setters
}
不幸的是,我收到了这个错误:
引起:org.hibernate.tool.schema.spi.SchemaManagementException:无法对JDBC目标执行模式管理[alter table rate add constraint FK8totaejp8tp48clsoikn05fn2 foreign key(answer_id)reference answers(answer_id)]
我用Google搜索了大约一个小时但我找不到解决方案。你能帮助我吗?我为什么要这个?
另外:spring.jpa.hibernate.ddl-auto = update,如果我关闭它,它解决了问题,但我不想这样做只是因为这个。
Answer类看起来像这样:
@Entity
@Table(name = "answer")
public class Answer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "answer_id")
private Long id;
@Column(name = "answer_text")
@NotEmpty
private String answerText;
@ManyToOne
@JoinColumn(name = "user_id")
private User answerCreator;
@ManyToOne
@JoinColumn(name = "question_id")
private Question question;
@Column(name = "answer_creation_date")
@NotNull
private Date creationDate = new Date();
@Column(name = "answer_rate")
private Long answerRate = 1l;
//plus getters-setters
}
用户类:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Long id;
@Column(name = "email")
@Email(message = "*Please provide a valid Email")
@NotEmpty(message = "*Please provide an email")
private String email;
@Column(name = "password")
@Length(min = 5, message = "*Your password must have at least 5 characters")
@NotEmpty(message = "*Please provide your password")
@Transient
private String password;
@Column(name = "active")
private int active;
@ManyToMany()
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
//Plus getters-setters
}
答案 0 :(得分:0)
尝试通过带有@JoinColumn批注的referencedColumnName传递名称。它对我有用。
@ManyToOne(optional = false)
@JoinColumn(name = "userTypeId", referencedColumnName = "id",nullable = false)
private UserType userType;
我的实体是User和UserType,其中User具有属性“ userTypeId”作为外键。