Java – Could not execute JDBC batch update:Exception in thread “main” org.hibernate.exception.ConstraintViolationException:

hibernatejavaMySQL

I am new to hibernate programming Please help me out, I have problem while executing the below files.I am using MySQL Database. My error is

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: insert into UserDetails (userName) values (?)
Hibernate: insert into UserDetails (userName) values (?)
Hibernate: insert into UserDetails (userName) values (?)
Hibernate: insert into vehicle (vehiName) values (?)
Hibernate: insert into vehicle (vehiName) values (?)
Hibernate: insert into UserDetails_vehicle (UserDetails_userID, vehi_vehiID) values (?, ?)
Hibernate: insert into UserDetails_vehicle (UserDetails_userID, vehi_vehiID) values (?, ?)
Hibernate: insert into UserDetails_vehicle (UserDetails_userID, vehi_vehiID) values (?, ?)

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.annt.java.UserTest.main(UserTest.java:34)
Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 'vehi_vehiID'
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
… 8 more

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

}

Best Answer

The error message is quite clear: there is a unique key violation constraint: you're inserting the same vehicle for two different users:

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

That is incorrect, since the association between User and Vehicle is a OneToMany association. A given Vehicle thus belongs to one and only one User. If you want to share a vehicle between users, then you need a ManyToMany association, and you need to remove the unique constraint on UserDetails_vehicle.vehi_vehiID on (Hibernate won't create it if you use it to generate the schema)