java 如何在Hibernate中从自引用实体中获取数据

h7wcgrx3  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(146)

I have two tables:

  1. medicaments_group enter image description here
  2. medicaments_group_join enter image description here
    There is my Entity class:
@Entity @Table(name = "medicaments_group") @Getter @Setter public class MedicamentGroup extends GenericDictionary {

    @Id
    private Long id;

    private boolean groupMain;

    @ManyToMany
    @JoinTable(
            name = "medicaments_group_join",
            joinColumns = @JoinColumn(name = "medicament_group_id"),
            inverseJoinColumns = @JoinColumn(name = "medicament_join_id")
    )
    private List<MedicamentGroup> childrens = new ArrayList<>();

    @ManyToMany
    @JoinTable(
            name = "medicaments_group_join",
            joinColumns = @JoinColumn(name = "medicament_join_id"),
            inverseJoinColumns = @JoinColumn(name = "medicament_group_id")
    )
    private List<MedicamentGroup> childrenOf; }

And i would like to return List of MedicamentGroup with List of its children, so i wrote query like that:

@Override
public List<MedicamentGroup> getGroupsAndItsChildren() {
    List<Tuple> query = Objects.requireNonNull(getQuerydsl())
            .createQuery()
            .from(medicamentGroup)
            .select(id, shortName, childrens)
            .where(groupMain.isFalse())
            .fetch();


    return query.stream().map(tuple -> {
        MedicamentGroup group = new MedicamentGroup();
        group.setId(tuple.get(id));
        group.setShortName(tuple.get(shortName));
        group.setChildrens(tuple.get(childrens));
        return group;
    }).toList();
}

And now, when i try to call this method from my repository layer i got an error:
Hibernate: select medicament0_.id as col_0_0_, medicament0_.short_name as col_1_0_, . as col_2_0_, medicament2_.id as id1_20_, medicament2_.active as active2_20_, medicament2_.name as name3_20_, medicament2_.short_name as short_na4_20_, medicament2_.group_main as group_ma5_20_ from medicaments_group medicament0_ inner join medicaments_group_join childrens1_ on medicament0_.id=childrens1_.medicament_group_id inner join medicaments_group medicament2_ on childrens1_.medicament_join_id=medicament2_.id where medicament0_.group_main=? 2023-04-03 20:12:23.563 WARN 46674 --- [nio-8089-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42601 2023-04-03 20:12:23.563 ERROR 46674 --- [nio-8089-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: syntax error at or near "." Position: 74 2023-04-03 20:12:23.579 ERROR 46674 --- [nio-8089-exec-3] p.k.c.w.f.ErrorResponseFactoryImpl
: Error response
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:259) ~[spring-orm-5.3.22.jar:5.3.22] at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233) ~[spring-orm-5.3.22.jar:5.3.22] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551) ~[spring-orm-5.3.22.jar:5.3.22] at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.3.22.jar:5.3.22] at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.3.22.jar:5.3.22] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152) ~[spring-tx-5.3.22.jar:5.3.22] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.22.jar:5.3.22] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.22.jar:5.3.22]
So could you help me and tell me what is wrong with my code?

sirbozc5

sirbozc51#

在你的实体类中,像这样进行Map:

@Entity @Table(name = "medicaments_group") @Getter @Setter public class MedicamentGroup extends GenericDictionary {

    @Id
    private Long id;

    private boolean groupMain;

     @ManyToMany(cascade = CascadeType.ALL)
    private List<MedicamentGroup> childrens = new ArrayList<>();

     @ManyToMany(cascade = CascadeType.ALL)
    private List<MedicamentGroup> childrenOf; }

相关问题