我犯错的原因是什么?我必须实体。他们的关系是多对多的。
前置:
@Entity(name = "prepod")
public class Prepod {
private Long id;
List<Student> students = new ArrayList<Student>();
@ManyToMany
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long i) {
id = i;
}
}
学生:
@Entity
@Table(name="Student")
public class Student {
private Long id;
private String name;
private Long age;
private List<Prepod> prepods = new ArrayList<Prepod>();
@ManyToMany(mappedBy="students")
public List<Prepod> getPrepods() {
return prepods;
}
public void setPrepods(List<Prepod> prepods) {
this.prepods = prepods;
}
public Student(){
name = null;
}
public Student(Student s){
name = s.getName();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
@Column (name = "id")
public Long getId() {
return id;
}
@Column(name="name")
public String getName(){
return name;
}
@Column(name="age")
public Long getAge(){
return age;
}
public void setId(Long i){
id = i;
}
public void setName(String s){
name = s;
}
public void setAge(Long age){
this.age = age;
}
}
调用代码:
public static void main(String[] args){
Prepod prepod = new Prepod();
prepod.setId(1l);
Student student = new Student();
student.setId(1l);
student.setName("qweqwe");
prepod.getStudents().add(student);
student.getPrepods().add(prepod);
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(prepod);
session.save(student);
session.getTransaction().commit();
}
异常文本:
Hibernate: insert into prepod default values
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:364)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:205)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:185)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:169)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
at logic.Main.main(Main.java:17)
表在数据库中创建,但数据不会插入到我的表中。
你能解释一下是什么原因吗?
如何解决?
更新
生成的架构: