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

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

汝楷
2023-03-14

我刚开始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异常:无法在org.hibernate.Exception.sqlstateconverter.convert(sqlstateconverter.convert(sqlstateconverter.java:96)在org.hibernate.Exception.jdbcexceptionHelper.convert(sqlstateconverter.java:66)在org.hibernate.JDBC.abstractBatcher.executeBatch(abstractBatcher.java:275)在org.hibernate.engine.actionQueue.executeAction(actionQueue.java:268)在org.hibernate.event.def.defaultflusheventlistener.java:321)处的org.hibernate.impl.sessionimpl.flush(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:在com.mysql.jdbc.PreparedStatement.ExecuteBatchSeriary(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.268)...8其他...

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

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

 类似资料: