Hibernate: Problem with mapping
I have a problem with hibernate. (((
!!! Image with Database Schema !!!
Mapping (for getter's/setter's i use lo开发者_JAVA百科mbok)
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;
}
I can read any data from any table, but cannot write into intervals table ((
The error message is: *Field 'users_on_projects_id' doesn't have a default value* And hibernate generate this SQL code: insert into intervals (finish_time, interval_type_id, start_time) values (?, ?, ?)
What's wrong with mapping?
Your mapping looks a bit strange (but may I understand is wrong). What I found so strange is that both relations:
- User -> Interval
- Project -> Interval
are stored in the SAME join table "intervals" and in both cases the one case the JoinColumn is "users_on_projects_id".
For my it looks like a copy and past bug.
精彩评论