1

my problem is that one row added successfully but when adding more data it shows an error given below. there is duplicate entry but i want to insert two laptop data for the same student. Im beginner in hibernate please guide me sir.

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190) at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3587) at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:103) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) at HibRelation.App.main(App.java:43) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187) ... 13 more 

this is my student.java class

package HibRelation; import java.util.*; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.OneToOne; @Entity public class Student { @Id private int RollName; private String Lname; private int marks; @OneToMany(mappedBy = "std") private List<Laptop> laptop = new ArrayList<Laptop>(); public int getRollName() { return RollName; } public void setRollName(int RollName) { this.RollName = RollName; } public String getLname() { return Lname; } public void setLname(String Lname) { this.Lname = Lname; } public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public List<Laptop> getLaptop() { return laptop; } public void setLaptop(List<Laptop> laptop) { this.laptop = laptop; } @Override public String toString() { return "Student{" + "RollName=" + RollName + ", Lname=" + Lname + ", marks=" + marks + '}'; } } 

and this is my Laptop.java class

import java.util.*; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; @Entity public class Laptop { @Id private int id; private String lname; @ManyToOne private Student std; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public Student getStd() { return std; } public void setStd(Student std) { this.std = std; } } 

and this is main logic App.java

public class App { public static void main(String[] args) { Student student=new Student(); student.setRollName(102); student.setLname("Dinu"); student.setMarks(95); Laptop lap= new Laptop(); lap.setId(1); lap.setLname("hp"); student.getLaptop().add(lap); lap.setStd(student); Configuration con=new Configuration().configure().addAnnotatedClass(Student.class).addAnnotatedClass(Laptop.class); StandardServiceRegistry registry=new StandardServiceRegistryBuilder().applySettings(con.getProperties()).build(); SessionFactory sfact=con.buildSessionFactory(registry); Session session = sfact.openSession(); session.beginTransaction(); session.save(student); session.save(lap); session.getTransaction().commit(); } } 

Configuration.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedata</property> <property name="hibernate.connection.username">root</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>

3
  • 1
    Your IDs are not auto-generated. And you always use 1 as the ID. So yes, the second time, you get an exception since IDs are supposed to be unique. So, either auto-generate the IDs (read the documentation, that's what it's for), or generate IDs yourself bu make sure they're unique (you generally don't want to do that, unless you use UUIDs). Commented Jul 29, 2018 at 13:04
  • 1
    Duplicate entry '1' for key 'PRIMARY' Error speaks for itself. Commented Jul 29, 2018 at 13:05
  • yeah thanks but i want to insert many laptop for one student in one by one Commented Jul 29, 2018 at 14:50

2 Answers 2

1

The error is caused by the duplication of a primary key in laptop table. A primary key is a column which uniquely identifies any row of the table. if you want to insert more than once keep changing the id value of laptop.So when you run the program for next time change the value of Id. Like

laptop.setId(2); //or something else which is not present in the table already 

if you want to add the same laptop into a new student then retrieve that laptop first for e.g

 Laptop theLaptop = Session.get(Laptop.class,theId) // theId is the laptopid student.addLaptop(theLaptop); session.save(student); 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks sir but what if i want to insert different student but same laptop details ???
Go through this article you will get to know more clearly krams915.blogspot.com/2011/03/spring-hibernate-one-to-many.html
0

I would suggest using @GeneratedValue annotation on field id. Your error is due to violation of primary key constraint on field id. You must avoid setting id while saving objects ideally.

If you want to add same laptop object every time to different students, initialize your fields in laptop entity class, add a constructor and call that constructor every time while setting a laptop for any student.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.