gpt4 book ai didi

java - MySQL: org.hibernate.HibernateException: 找到超过一行的给定标识符 3

转载 作者:行者123 更新时间:2023-11-28 23:48:08 25 4
gpt4 key购买 nike

我有两个表,两个表之间存在一对一关系。

A表:

@Entity
public class A implements GrantedAuthority {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer aId;

@NotEmpty
@Column(unique = true, nullable = false)
private String name;

@JsonIgnore
@OneToOne(mappedBy = "a", targetEntity = B.class, fetch = FetchType.LAZY)
private B b;

getter and setter

用户表:

@Entity
@Table
public class B {

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(columnDefinition = "VARCHAR(50)")
private String bUUId;

@Column(unique = true, columnDefinition = "VARCHAR(30)", nullable = false)
private String name;

@Column(columnDefinition = "VARCHAR(50)", nullable = false)
private String x;

@JsonIgnore
@OneToOne(targetEntity = A.class, fetch = FetchType.EAGER)
@JoinColumn(name = "aId", referencedColumnName = "aId")
private A a;

//don't save this value into DB
@Transient
private Set<A> aSet;

我调用了这个查询

@Override
public B findByBname(String name) {

String query = "FROM B b WHERE b.name = :name";
try {
return (B) entityManager.createQuery(query).setParameter("name",name).getSingleResult();
} catch (SecurityException | IllegalStateException | RollbackException e) {
LOGGER.info(e.getMessage());

错误日志:

org.springframework.orm.jpa.JpaSystemException: More than one row with the given identifier was found: 3, for class: com.z.server.model.B; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 3, for class: com.z.server.model.B
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:310)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy86.findBBname(Unknown Source)
at com.z.server.controller.BController.findB(BController.java:64)

当我有一个带有此 Bname 的条目进入数据库时​​,出现了此异常。

有没有人有什么想法

干杯

最佳答案

您正在调用 getSingleResult(),这意味着您的查询只需要一个结果:

FROM B b WHERE b.name = :name

您的表中的名称列下似乎有多个具有相同值的行。

如果它返回多行,您可以调用 getResultList() 而不是获取 List 对象。或者您必须确保表中的数据在名称列下不包含重复值。

关于java - MySQL: org.hibernate.HibernateException: 找到超过一行的给定标识符 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33154301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com