emoleumassi
emoleumassi

Reputation: 5139

MySQL: org.hibernate.HibernateException: More than one row with the given identifier was found 3

I have two table with a oneToOne Relationship between the two tables.

The A table:

@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

The user table:

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

i called this query

@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());

And the error log:

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)

a got this execption when i have one entry with this Bname into database.

has anyone any ideas

Cheers

Upvotes: 1

Views: 2144

Answers (1)

Ish
Ish

Reputation: 4154

You are invoking getSingleResult() which means only one result is expected from your query:

FROM B b WHERE b.name = :name

It seems like you have multiple rows having the same value under name column in your table.

You can either invoke getResultList() instead to get a List object if it returns multiple rows. Or you have to ensure data in your table is doesn't contain duplicate values under name column.

Upvotes: 1

Related Questions