无法执行JDBC批量更新:线程“main”中的异常org.hibernate.exception.ConstraintViolationException:

时间:2014-01-24 07:32:29

标签: java mysql hibernate

我是hibernate编程的新手请帮助我,执行以下文件时遇到问题。我正在使用MySQL数据库。我的错误是

  

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。       SLF4J:默认为无操作(NOP)记录器实现       SLF4J:有关详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder。       Hibernate:插入UserDetails(userName)值(?)       Hibernate:插入UserDetails(userName)值(?)       Hibernate:插入UserDetails(userName)值(?)       Hibernate:插入车辆(vehiName)值(?)       Hibernate:插入车辆(vehiName)值(?)       Hibernate:插入UserDetails_vehicle(UserDetails_userID,vehi_vehiID)值(?,?)       Hibernate:插入UserDetails_vehicle(UserDetails_userID,vehi_vehiID)值(?,?)       Hibernate:插入UserDetails_vehicle(UserDetails_userID,vehi_vehiID)值(?,?)

     

线程“main”中的异常org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新       在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)       在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)       在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)       在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)       在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)       在org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)       在org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)       在org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)       在org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)       在org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)       在com.annt.java.UserTest.main(UserTest.java:34)       引起:java.sql.BatchUpdateException:键'vehi_vehiID'的重复条目'1'       在com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)       在com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)       在org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)       在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)       ......还有8个

UserDetails.java

@Entity
public class UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int userID;
    @OneToMany
    private List<vehicle> vehi = new ArrayList<vehicle>();

    public List<vehicle> getVehi() {
        return vehi;
    }

    public void setVehi(List<vehicle> vehi) {
        this.vehi = vehi;
    }

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

vehicle.java

 @Entity
    public class vehicle {

        @Id
        @GeneratedValue
        private int vehiID;

        public int getVehiID() {
            return vehiID;
        }

        public void setVehiID(int vehiID) {
            this.vehiID = vehiID;
        }

        public String getVehiName() {
            return vehiName;
        }

        public void setVehiName(String vehiName) {
            this.vehiName = vehiName;
        }
        private String vehiName;

    }

usertest.java

public class UserTest {

    public static void main(String[] args) {
        vehicle veh1 = new vehicle();
        veh1.setVehiName("car");
        vehicle veh2 = new vehicle();
        veh2.setVehiName("bus");
        UserDetails user1 = new UserDetails();
        UserDetails user2 = new UserDetails();
        user1.setUserName("user2");
        user2.setUserName("user2");
        UserDetails user3 = new UserDetails();
        user3.setUserName("user3");
        user1.getVehi().add(veh1);
        user2.getVehi().add(veh2);
        user3.getVehi().add(veh1);
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction();
        ss.save(user1);
        ss.save(user2);
        ss.save(user3);
        ss.save(veh1);
        ss.save(veh2);
        ss.getTransaction().commit();
        ss.close();
    }

}

3 个答案:

答案 0 :(得分:6)

错误消息非常明确:存在唯一的密钥违规限制:您为两个不同的用户插入相同的车辆:

user1.getVehi().add(veh1);
//...
user3.getVehi().add(veh1);

这是不正确的,因为User和Vehicle之间的关联是OneToMany关联。因此,给定车辆属于一个且仅一个用户。如果您想在用户之间共享车辆,那么您需要一个ManyToMany关联,并且您需要删除UserDetails_vehicle.vehi_vehiID上的唯一约束(如果您使用它来生成架构,Hibernate将不会创建它)

答案 1 :(得分:0)

您将veh1影响到2个不同的用户,因此您需要一个Many2Many协会,而不是One2Many:

@OneToMany
private List vehi=new ArrayList();

Many2Many:一个用户可能会受到许多车辆的影响而一个车辆可能会受到许多用户的影响 One2Many:一个用户可能会受到许多车辆的影响,但One Vehicule只能由一个用户拥有

这就是你得到完整性异常的原因

答案 2 :(得分:0)

在进行关系运输时,对象值必须不同  在user3.getVehi()中给出不同的值.add(veh1)