MySQLIntegrityConstraintViolationException

时间:2014-09-27 12:01:50

标签: java hibernate

我在hibernate中实现了一对多的关系bt我得到了一个重复键的错误,所以有点混淆如何做 如果有人能纠正我的错误,那将是一个帮助,实际上我已经进入休眠状态了几天

这是我的大学课程

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

}

我的主要课程

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架构导出 Hibernate:alter table Students_College删除外键FK_qk8e1fl454umkjvarmovmdgrp Hibernate:alter table Students_College drop外键FK_n2cycly15ecddgky71345r3u Hibernate:drop table如果存在College Hibernate:drop table如果存在学生 Hibernate:drop table如果存在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:创建表Students_College(Students_college_id整数不为null,college_college_id整数不为null) Hibernate:alter table Students_College添加约束UK_qk8e1fl454umkjvarmovmdgrp unique(college_college_id) Hibernate:alter table Students_College添加约束FK_qk8e1fl454umkjvarmovmdgrp外键(college_college_id)引用College(college_id) Hibernate:alter table Students_College添加约束FK_n2cycly15ecddgky71345r3u外键(Students_college_id)引用学生(college_id) 2014年9月27日下午12:21:01 org.hibernate.tool.hbm2ddl.SchemaExport执行 信息:HHH000230:架构导出完成 Hibernate:插入学生(名字,姓氏)值(?,?) Hibernate:插入College(位置,名称)值(?,?) Hibernate:插入学生(名字,姓氏)值(?,?) Hibernate:插入Students_College(Students_college_id,college_college_id)值(?,?) Hibernate:插入Students_College(Students_college_id,college_college_id)值(?,?) 2014年9月27日下午12:21:01 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 警告:SQL错误:1062,SQLState:23000 2014年9月27日下午12:21:01 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 错误:重复输入&#39; 1&#39;关键词&#39; UK_qk8e1fl454umkjvarmovmdgrp&#39; 2014年9月27日下午12:21:01 org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl发布 信息:HHH000010:在批量发布时,它仍然包含JDBC语句 线程&#34; main&#34;中的例外情况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)     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)     在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)     在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)     at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)     at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)     在com.hibernate.arjun3.Main1.main(Main1.java:31) 引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目&#39; 1&#39;关键词&#39; UK_qk8e1fl454umkjvarmovmdgrp&#39;     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)     在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)     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)     ......还有12个

希望收到你们所有经验丰富的人的回复 感谢你

1 个答案:

答案 0 :(得分:1)

你的映射毫无意义。

首先,我发现一个学生有很多大学,而不是反过来有点奇怪:一所大学有很多学生。让我们忽略这一点,看看技术问题:

@ElementCollection
@OneToMany(cascade = CascadeType.ALL)

如果它是OneToMany,它不能是元素集合。 {@ 1}}应该删除。

@ElementCollection

为什么这个实体名为@Entity public class Students { ?每个实例代表一个学生。因此,该类应命名为Students

Student

为什么学生会被大学身份证明?它应该是学生证。该字段应尊重Java命名约定。您应该将其重命名为@Id @GeneratedValue private int college_id;

id

现在这是一个包含多个学院的集合,您将其命名为@JoinTable(name="College_Students" , joinColumns=@JoinColumn(name="College_Id")) private Collection<College> college = new ArrayList<College>(); ,而不是college。当它应该是复数时使用单数形式,反之亦然会使你的代码非常混乱。

但主要问题如下:你说Hibernate学生和学院之间存在OneToMany关联。所以一所大学可能只属于一名学生。否则,它必须是ManyToMany。然而,这就是你的代码所做的事情:

colleges

同一所大学加入了2名不同的学生。