1

我犯错的原因是什么?我必须实体。他们的关系是多对多的。

前置:

@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)

表在数据库中创建,但数据不会插入到我的表中。

你能解释一下是什么原因吗?

如何解决?

更新

生成的架构:

生成的架构:

4

2 回答 2

0

One of your table has some constraint (e.g unique) which is preventing the insert query to be executed. Check your table constraints and modify your code.

于 2013-09-11T15:28:20.787 回答
-1

由于这个辞职约束,已经表插入了违规异常 EX:表是 emp 1,ramesh,1000 2,vasu,3000 note 1,shekar,6000 这是辞职

于 2013-12-12T04:19:58.430 回答