当前位置: 首页 > 知识库问答 >
问题:

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

邰德业
2023-03-14

我刚开始使用hibernate编程,请帮帮我,我在执行下面的文件时遇到了问题。我使用的是MySQL数据库。我的错误是

SLF4J:未能加载类“org.slf4j.impl.StatibloggerBinder”。slf4j:默认为无操作(NOP)记录器实现slf4j:有关更多细节,请参见http://www.slf4j.org/codes.html#staticloggerbinder。Hibernate:插入到UserDetails(用户名)值(?)Hibernate:插入到UserDetails(用户名)值(?)Hibernate:插入到UserDetails(用户名)值(?)Hibernate:插入车辆(vehiName)值(?)Hibernate:插入车辆(vehiName)值(?)Hibernate:插入到userdetails_vehice(UserDetails_userID,vehi_vehiID)值(?,?)Hibernate:插入到userdetails_vehice(UserDetails_userID,vehi_vehiID)值(?,?)Hibernate:插入到userdetails_vehice(UserDetails_userID,vehi_vehiID)值(?,?)

线程“main”org.hibernate.Exception.constraintViolationException:无法在org.hibernate.Exception.jdbcExceptionHelper.convert(sqlstateConverter.convert(sqlstateConverter.java:96)在org.hibernate.Exception.JDBC.exceptionHelper.convert(jdbcExceptionHelper.convert(jdbcExceptionHelper.java:66)在在org.hibernate.event.def.DefaultFlushEventListener.onflush(DefaultFlushEventListener.java:51),org.hibernate.impl.sessionImpl.flush(SessionImpl.java:1216),org.hibernate.impl.sessionImpl.managedFlushpdateException:在com.mysql.jdbc.preparedStatement.executeBatchSeryl(preparedStatement.java:1669)在org.hibernate.jdbc.preparedStatement.executeBatch(preparedStatement.java:1085)在org.hibernate.jdbc.batchingBatcher.doExecuteBatch(batchingBatcher.java:70)在org.hibernate.jdbc.abstractBatcher.executeBatch(

@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;
    }

}
 @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;

    }
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();
    }

}

共有1个答案

曹理
2023-03-14

错误消息很清楚:有一个唯一的密钥冲突约束:您为两个不同的用户插入了相同的车辆:

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

这是不正确的,因为用户和车辆之间的关联是一个OneToMany关联。因此,给定的车辆只属于一个用户。如果您希望在用户之间共享一个交通工具,那么您需要一个ManyToMany关联,并且您需要删除userdetails_vehicle.vehi_vehiidon上的唯一约束(如果您使用它来生成模式,Hibernate不会创建它)

 类似资料: