我在hibernate中實現了一對多的關係bt我得到了重複鍵的錯誤,所以有點混淆,以至於該怎麼做 如果有人能糾正我的錯誤,那麼它實際上只是一些幫助,天即時進入休眠MySQLIntegrityConstraintViolationException
這是我的大專班
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" , [email protected](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;
}
}
我的主類
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();
}
}
的錯誤:
信息:HHH000227:運行就是hbm2ddl架構出口 休眠:ALTER TABLE Students_College刪除外鍵FK_qk8e1fl454umkjvarmovmdgrp 休眠:ALTER TABLE Students_College刪除外鍵FK_n2cycly15ecddgky71345r3u 休眠:降表,如果存在學院 休眠:DROP TABLE是否存在學生 休眠:降表,如果存在Students_College 休眠:CREATE TABLE學院(college_id整數NOT NULL AUTO_INCREMENT,位置爲varchar(255 ),name varchar(255),主鍵(college_id)) Hibernate:創建表學生(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添加約束UK_qk8e1fl454umkjvarmovmdgrp unique(college_college_id) Hib ernate:ALTER TABLE Students_College添加約束FK_qk8e1fl454umkjvarmovmdgrp外鍵(college_college_id)引用學院(college_id) 休眠:ALTER TABLE Students_College添加約束FK_n2cycly15ecddgky71345r3u外鍵(Students_college_id)引用學生(college_id) 2014年9月27日下午12時21分01秒組織。 hibernate.tool.hbm2ddl.SchemaExport execute 信息:HHH000230:模式導出完成 Hibernate:插入學生(名字,姓氏)值(?,?) Hibernate:插入學院(位置,名稱)值(?,?) (?,?) 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(?,?) 2014年9月27日12:21:01 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN:SQL錯誤:1062,SQLState:23000 2014年9月27日12 :21:01 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 錯誤:重複條目'1'鍵'UK_qk8e1fl454umkjvarmovmdgrp' 2014年9月27日下午12時21分01秒org.hibernate.engine.jdbc.batch .internal.AbstractBatchImpl版本 信息:HHH000010:在批量發佈時,它仍包含JDBC語句 線程「main」中的異常org.hibernate.exception.ConstraintViolationException:無法在org.hibernate.exception.internal.SQLExceptionTypeDelegate中執行語句 。轉換(SQLExceptionTypeDelegate。的java:72) 在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) 在有機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.engin 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。在org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit()中, AbstractTransactionImpl.java:177) at com.hibernate.arjun 3.Main1.main(Main1.java:31) 原因:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重複鍵'UK_qk8e1fl454umkjvarmovmdgrp'的條目'1'在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本機方法) 在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知來源) 在sun.reflect.DelegatingConstructorAccessorImpl.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) 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更多
希望從u都經歷了PPL 回覆,感謝您
您是否使用序列表來生成ID?它可能是序列表仍然分配ID 1,但該ID已被實例佔用。 – 2014-09-27 12:10:12