Java MySQLIntegrityConstraintViolationException
我在hibernate bt中实现了一对多关系,但我得到了一个复制密钥的错误,所以有点困惑该怎么做 如果有人能纠正我的错误,这将是一个grt的帮助,它实际上只是好几天我进入冬眠 这是我的大学课Java MySQLIntegrityConstraintViolationException,java,hibernate,Java,Hibernate,我在hibernate bt中实现了一对多关系,但我得到了一个复制密钥的错误,所以有点困惑该怎么做 如果有人能纠正我的错误,这将是一个grt的帮助,它实际上只是好几天我进入冬眠 这是我的大学课 package com.hibernate.arjun3; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public cla
package com.hibernate.arjun3;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@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) {
this.name = 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;
}
}
这是我的学生班
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.Id;
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;
}
}
错误:
信息:HH000227:正在运行hbm2ddl架构导出
Hibernate:alter table Students_College drop外键FK_QK8E1FL454UMKJVARMODGRP
Hibernate:alter table Students_College drop外键FK_n2cycly15ecddgky71345r3u
Hibernate:如果存在,则删除表
休眠:删除表(如果存在)
Hibernate:如果存在,则删除表
Hibernate:创建表College(College\u id integer非null自动增量,location varchar(255),name varchar(255),主键(College\u id))
Hibernate:创建表Students(college\u id integer非null自动增量、firstname varchar(255)、lastname varchar(255)、主键(college\u id))
Hibernate:创建表Students\u College(Students\u College\u id整数不为null,College\u College\u id整数不为null)
Hibernate:alter table Students_College添加约束UK_qk8e1fl454umkjvarmovmgrp unique(College_College_id)
Hibernate:alter table Students_College添加约束FK_qk8e1fl454umkjvarmovmdgrp外键(College_College_id)引用College(College_id)
Hibernate:alter table Students\u College add constraint FK\u n2cycly15ecddgky71345r3u外键(Students\u College\u id)引用Students(College\u id)
2014年9月27日12:21:01 PM org.hibernate.tool.hbm2ddl.SchemaExport执行
信息:HH000230:架构导出已完成
Hibernate:在学生中插入(名字、姓氏)值(?,)
Hibernate:插入学院(位置、名称)值(?,)
Hibernate:在学生中插入(名字、姓氏)值(?,)
Hibernate:插入学生学院(学生学院id,学院学院id)值(?,)
Hibernate:插入学生学院(学生学院id,学院学院id)值(?,)
2014年9月27日12:21:01 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper日志异常
警告:SQL错误:1062,SQLState:23000
2014年9月27日12:21:01 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper日志异常
错误:键“UK_qk8e1fl454umkjvarmovmdgrp”的重复条目“1”
2014年9月27日12:21:01 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl发布
信息:HH000010:在批处理发布时,它仍然包含JDBC语句
线程“main”org.hibernate.Exception.ConstraintViolationException中出现异常:无法执行语句
位于org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
位于org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
位于org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
位于org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
位于org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
位于org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
位于org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1311)
位于org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)
位于org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
位于org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
位于org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
位于org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
位于org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
位于org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
位于org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
位于org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
位于com.hibernate.arjun3.Main1.main(Main1.java:31)
原因:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:键“UK_qk8e1fl454umkjvarmovmgrp”的重复条目“1”
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源)
位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源)
位于java.lang.reflect.Constructor.newInstance(未知源)
位于com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
位于com.mysql.jdbc.Util.getInstance(Util.java:383)
位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049)
在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4208)
在com.mysql.jdbc.MysqlIO.checkErrorPacket上(MysqlIO.java:4140)
位于com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2597)
位于com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
位于com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2826)
位于com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
位于com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
位于com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
位于com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
位于org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 还有12个
希望你们都能回复ppl
谢谢你你的映射毫无意义 首先,我觉得有点奇怪,一个学生有很多学院,而不是相反:一个学院有很多学生。让我们忽略这一点,看看技术问题
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();
}
@ElementCollection
@OneToMany(cascade = CascadeType.ALL)
@Entity
public class Students {
@Id
@GeneratedValue
private int college_id;
@JoinTable(name="College_Students" , joinColumns=@JoinColumn(name="College_Id"))
private Collection<College> college = new ArrayList<College>();
students.getCollege().add(colg);
students2.getCollege().add(colg);