我的数据库中有这些(简化的)表
我已经用这两个(简化的)类映射了表
公司
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinTable(name="company_employee",
joinColumns={@JoinColumn(name="company_id")},
inverseJoinColumns={@JoinColumn(name="employee_id")})
@OrderBy("name ASC")
private SortedSet<Employee> employees = new TreeSet<Employee>();
// contructors + getters + setters
}
@Entity
public class Employee implements Comparable<Employee> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(fetch = FetchType.LAZY, optional = true)
@JoinTable(name="company_employee",
joinColumns={@JoinColumn(name="employee_id")},
inverseJoinColumns={@JoinColumn(name="company_id")}
)
private Company company;
@Override
public int compareTo(Employee anotherEmployee) {
return this.name.compareTo(anotherEmployee.name);
}
// contructors + getters + setters + equals + hashcode
}
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:86)
at es.rubioric.hibernate.MainTest.main(MainTest.java:43)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:67)
... 1 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1314)
at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:50)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:447)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:333)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:335)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1224)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:464)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2890)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2266)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:230)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61)
... 1 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '32-80' for key 'PRIMARY'
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:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2643)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 18 more
public static void main(String[] args) throws ParseException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestDB");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Company c = new Company("Nike");
Employee e1 = new Employee("Anthony");
e1.setCompany(c); // line 26
Employee e2 = new Employee("Zenobia");
e2.setCompany(c); // line 28
Employee e3 = new Employee("Chuck");
e3.setCompany(c); // line 30
Employee e4 = new Employee("Bernard");
e4.setCompany(c); // line 32
c.getEmployees().add(e1);
c.getEmployees().add(e2);
c.getEmployees().add(e3);
c.getEmployees().add(e4);
em.persist(c);
tx.commit();
} finally {
em.close();
}
}
Hibernate: insert into Company (name) values (?)
Hibernate: insert into Employee (name) values (?)
Hibernate: insert into company_employee (company_id, employee_id) values (?, ?)
Hibernate: insert into Employee (name) values (?)
Hibernate: insert into company_employee (company_id, employee_id) values (?, ?)
Hibernate: insert into Employee (name) values (?)
Hibernate: insert into company_employee (company_id, employee_id) values (?, ?)
Hibernate: insert into Employee (name) values (?)
Hibernate: insert into company_employee (company_id, employee_id) values (?, ?)
Hibernate: insert into company_employee (company_id, employee_id) values (?, ?)
如果我注释第26、28、30和32行(上面标记的),同样的代码可以完美地工作。但我想知道为什么会产生异常。为什么是复制的钥匙?
提前谢了。
重复是由于您的公司-员工关系在两个实体中重复造成的。每一个都被设置为单向1:M(一方出于某种原因使用1:1),并且两者都使用相同的联接表,当JPA为双方插入条目时,会导致重复。
解决办法是将一边标记为“mappedby”,另一边标记为“mappedby”。
@OneToMany( mappedBy="company", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@OrderBy("name ASC")
private SortedSet<Employee> employees = new TreeSet<Employee>();
问题内容: 所以我的MySQL数据库表现得有些奇怪。这是我的桌子: 当我尝试插入表时出现此#1062错误。因此,我进一步研究了它,并意识到当我尝试在名称和共享值相同的表中插入值时,它将返回#1062错误。例如,如果我插入: 它将返回错误。但是,如果我将共享数更改为6,它将运行正常。是因为我的专栏之一可能是唯一的,还是仅与mysql有关? 问题答案: 您需要删除作为OR
问题内容: 在通过MySQL数据库使用hibernate和jpa的Spring MVC应用程序中,每当我尝试保存包括子实体的父实体时,都会收到有关子实体的以下错误消息: 在这里,并引用子实体的组合主键的两个部分。 如何解决此错误? 在父实体中定义实体之间的关系的方法如下: 这是在子实体中定义关系的方式: 单击此链接可以查看完整的堆栈跟踪。可以在此链接上找到实体 的完整代码。 可以在此链接上读取实体
问题内容: 尝试在数据库中插入值时,mysql表出现问题。 我遵循了本教程 http://sqllessons.com/categories.html 并像教程中的表格一样创建表格 表格代码 错误的 SQL查询:编辑编辑 帮我解决这个问题。 问题答案: 声明要自动递增的值,不要插入它。所以: 接着:
问题内容: 我在MySQL 5.5.24中有下表 它具有大量数据,并且其他两个表具有和并具有多对一关系。 现在,我需要更改结构并在表中引入单独的主键,同时仍保留现有的关系和数据。为此,我执行了以下查询: 不幸的是,我的第二次查询失败,并出现以下错误: 1062-键“ PRIMARY”的条目“ 0”重复 有人可以指出这个问题吗?我想问题是现有的关系,但是我不想丢失具有数千行的现有关系或数据。有什么方
问题内容: 尝试以下操作时,我在此错误消息上遇到了问题: 错误代码:1062。键“ PRIMARY”的条目“ 1”重复 我没有auto_increment数据,请帮助我! 这是与表格相关的, UFFICIO-INFORMAZIONI 插入 问题答案: 该 主 为什么已经产生错误的原因是因为已经有一个现有值的列在其中将其定义为( 值是唯一 在要插入的表格)。 为什么不将列设置为? 当您插入记录时,您
HTTP状态500-请求处理失败;嵌套异常是org.springframework.dao.dataIntegrityViolationException:键“groups_groupid”的重复条目“2”;SQL[N/A];约束[null];嵌套异常是org.hibernate.exception.constraintViolationException:键“groups_groupid”的重复