我遇到了hibernate的问题。 (((
!!! Image with Database Schema !!!
映射 (对于getter's / setter,我使用lombok)
User.java
@Entity
@Table(name = "users")
public @Data class User
{
// Base Mapping
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
@Getter @Setter Long user_id;
@Column(name = "user_name")
private @Getter @Setter String user_name;
@Column(name = "login")
private @Getter @Setter String login;
@Column(name = "password")
private @Getter @Setter String password;
// Releations Mapping
// Releations With Project
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="users_on_projects",
joinColumns=@JoinColumn(name="user_id"),
inverseJoinColumns=@JoinColumn(name="project_id") )
private @Getter @Setter List<Project> projects = new Vector();
@ManyToMany
@JoinTable(name="intervals",
joinColumns=@JoinColumn(name="users_on_projects_id"),
inverseJoinColumns=@JoinColumn(name="interval_id") )
private @Getter @Setter List<Interval> intervals = new Vector();
}
Project.java
@Entity
@Table(name = "projects")
public @Data class Project
{
// Base Mapping
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="project_id")
private @Getter @Setter Long project_id;
@Column(name ="project_name")
private @Getter @Setter String project_name;
// Releations Mapping
// Releations With User
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="users_on_projects",
joinColumns=@JoinColumn(name="project_id"),
inverseJoinColumns=@JoinColumn(name="user_id") )
private @Getter @Setter List<User> users = new Vector();
@OneToMany
@JoinTable(name="intervals",
joinColumns=@JoinColumn(name="users_on_projects_id"),
inverseJoinColumns=@JoinColumn(name="interval_id") )
private @Getter @Setter List<Interval> intervals = new Vector();
}
Interval.java
@Entity
@Table(name = "intervals")
public class Interval
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "interval_id")
@Getter @Setter Long interval_id;
@Column(name ="start_time")
private @Setter @Getter
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
Date start_time;
@Column(name ="finish_time")
private @Setter @Getter
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
Date FinishTime;
@ManyToOne(cascade={CascadeType.ALL})
@JoinTable(name="users_on_projects",
joinColumns=@JoinColumn(name="users_on_projects_id"),
inverseJoinColumns=@JoinColumn(name="user_id") )
private @Getter @Setter User user;
@JoinTable(name="users_on_projects",
joinColumns=@JoinColumn(name="users_on_projects_id"),
inverseJoinColumns=@JoinColumn(name="project_id") )
private @Getter @Setter
@ManyToOne
Project project;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="interval_type_id")
private @Getter @Setter IntervalType intervalType;
}
IntervalType.java
@Entity
@Table(name = "interval_type")
public class IntervalType
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "interval_type_id")
@Getter @Setter Long interval_type_id;
@Column(name = "interval_type_name")
private @Getter @Setter String interval_type_name;
}
我可以从任何表中读取任何数据,但不能写入间隔表((
错误消息是: *字段'users_on_projects_id'没有默认值* 并且hibernate生成这个SQL代码: 插入间隔(finish_time,interval_type_id,start_time)值(?,?,?)
映射有什么问题?
答案 0 :(得分:0)
你的映射看起来有点奇怪(但我可能理解是错的)。我发现如此奇怪的是两种关系:
存储在SAME连接表“interval”中,在两种情况下,JoinColumn都是“users_on_projects_id”。
对于我而言,它看起来像是一个副本和过去的错误。