Hii this is arjun narahari
i have been watching ur tutorials on a frequent basis , i started up with hibernate recently
actually i m kinda stuck in between one to many relationship , it would be a grt help from u Koushik sir if u just guide me as in where i m making the mistake
this is my College class
package com.hibernate.arjun3;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@Entity
public class College {
private String name;
@Id
@GeneratedValue
private int college_id;
private String location;
public String getName() {
return name;
}
public void setName(String name) {
}
public int getCollege_id() {
return college_id;
}
public void setCollege_id(int college_id) {
this.college_id = college_id;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
this is my students class
package com.hibernate.arjun3;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;
@Entity
public class Students {
private String firstname;
private String lastname;
@Id
@GeneratedValue
private int college_id;
@ElementCollection
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name="College_Students" , joinColumns=@JoinColumn(name="College_Id"))
private Collection<College> college = new ArrayList<College>();
public Collection<College> getCollege() {
return college;
}
public void setCollege(Collection<College> college) {
this.college = college;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getCollege_id() {
return college_id;
}
public void setCollege_id(int college_id) {
this.college_id = college_id;
}
}
my Main class
package com.hibernate.arjun3;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main1 {
public static void main(String args[]) {
College colg = new College();
colg.setName("Vivekanand");
colg.setLocation("Chembur");
Students students = new Students();
students.setFirstname("Arjun");
students.setLastname("Narahari");
Students students2 = new Students();
students2.setFirstname("Sagar");
students2.setLastname("Abhyankar");
students.getCollege().add(colg);
students2.getCollege().add(colg);
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(students);
session.save(students2);
session.getTransaction().commit();
session.close();
factory.close();
}
}
This is the error i m getting after executing the main class
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: alter table Students_College drop foreign key FK_qk8e1fl454umkjvarmovmdgrp
Hibernate: alter table Students_College drop foreign key FK_n2cycly15ecddgky71345r3u
Hibernate: drop table if exists College
Hibernate: drop table if exists Students
Hibernate: drop table if exists Students_College
Hibernate: create table College (college_id integer not null auto_increment, location varchar(255), name varchar(255), primary key (college_id))
Hibernate: create table Students (college_id integer not null auto_increment, firstname varchar(255), lastname varchar(255), primary key (college_id))
Hibernate: create table Students_College (Students_college_id integer not null, college_college_id integer not null)
Hibernate: alter table Students_College add constraint UK_qk8e1fl454umkjvarmovmdgrp unique (college_college_id)
Hibernate: alter table Students_College add constraint FK_qk8e1fl454umkjvarmovmdgrp foreign key (college_college_id) references College (college_id)
Hibernate: alter table Students_College add constraint FK_n2cycly15ecddgky71345r3u foreign key (Students_college_id) references Students (college_id)
Sep 27, 2014 12:21:01 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into Students (firstname, lastname) values (?, ?)
Hibernate: insert into College (location, name) values (?, ?)
Hibernate: insert into Students (firstname, lastname) values (?, ?)
Hibernate: insert into Students_College (Students_college_id, college_college_id) values (?, ?)
Hibernate: insert into Students_College (Students_college_id, college_college_id) values (?, ?)
Sep 27, 2014 12:21:01 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
Sep 27, 2014 12:21:01 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '1' for key 'UK_qk8e1fl454umkjvarmovmdgrp'
Sep 27, 2014 12:21:01 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
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:211)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1311)
at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
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:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at com.hibernate.arjun3.Main1.main(Main1.java:31)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'UK_qk8e1fl454umkjvarmovmdgrp'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.Util.getInstance(Util.java:383)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4208)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4140)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2597)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2826)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 12 more
expecting ur reply asap
thanking you
Regards
Arjun Narahari