我有两个表,两个表之间有一个oneToOne关系。
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的条目进入数据库时,得到了这个execption。
有任何想法
干杯
答案 0 :(得分:1)
您正在调用getSingleResult()
,这意味着您的查询只需要一个结果:
FROM B b WHERE b.name = :name
您的表格中的名称列下似乎有多个具有相同值的行。
如果返回多行,您可以调用getResultList()
来获取List对象。或者您必须确保表中的数据不包含名称列下的重复值。