最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring - How to add Columns from Junction table to the Child - Stack Overflow

programmeradmin1浏览0评论

I am working with Spring Boot JPA Hibernate. I have @ManyToMany relationship and Junction Table. One Entity is called Entity and the other Button. And then Junction Table is called ENTITY_BUTTONS

This Junction Table has some additional Columns. When I get a List of Button Children I would also like these additional columns to be included. But I am not sure how to do that.

So basically I don't want just a list of children but list of some other structure that will contains columns from the child and additional Columns from Junction Table for that Child relation.

My goal is to just be able to get top Entitiy and then that all the other entities and their children are automatically picked up in a complete hierarchy.

@javax.persistence.Entity
@Table(name = "ENTITIES")
public class Entity {

    @ManyToMany(fetch = FetchType.LAZY)
    @WhereJoinTable( clause = "active = true and is_deleted = false")
    @JoinTable(
        name               = "ENTITY_BUTTONS",
        joinColumns        = @JoinColumn(name = "ENTITY_ID"),
        inverseJoinColumns = @JoinColumn(name = "BUTTON_ID")
    )
    private List<Button> buttons;

}


@Entity
@Table(name = "BUTTONS")
public class Button  { ... }

I am working with Spring Boot JPA Hibernate. I have @ManyToMany relationship and Junction Table. One Entity is called Entity and the other Button. And then Junction Table is called ENTITY_BUTTONS

This Junction Table has some additional Columns. When I get a List of Button Children I would also like these additional columns to be included. But I am not sure how to do that.

So basically I don't want just a list of children but list of some other structure that will contains columns from the child and additional Columns from Junction Table for that Child relation.

My goal is to just be able to get top Entitiy and then that all the other entities and their children are automatically picked up in a complete hierarchy.

@javax.persistence.Entity
@Table(name = "ENTITIES")
public class Entity {

    @ManyToMany(fetch = FetchType.LAZY)
    @WhereJoinTable( clause = "active = true and is_deleted = false")
    @JoinTable(
        name               = "ENTITY_BUTTONS",
        joinColumns        = @JoinColumn(name = "ENTITY_ID"),
        inverseJoinColumns = @JoinColumn(name = "BUTTON_ID")
    )
    private List<Button> buttons;

}


@Entity
@Table(name = "BUTTONS")
public class Button  { ... }
Share Improve this question edited Jan 20 at 13:01 ivoronline asked Jan 20 at 12:46 ivoronlineivoronline 1,0792 gold badges11 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I believe that you need to replace the ManyToMany with a pair of OneToMany on the explicitly defined Entity EntityButton.

@javax.persistence.Entity
@Table(name = "ENTITY_BUTTONS")
public class EntityButton {

    @EmbeddedId
    EntityButtonId entityButtonId;

    String anotherAttribute;

    String anotherAttribute2;
}

@javax.persistence.Entity
@Table(name = "ENTITIES")
public class Entity {

    @OneToMany(fetch = FetchType.LAZY)
    private List<EntityButton> entityButtons ;

}

@Entity
@Table(name = "BUTTONS")
public class Button  {     
    @OneToMany(fetch = FetchType.LAZY)
    private List<EntityButton> buttonEntities ;
    
 }

@Embeddable
public class EntityButtonId {
    private String entityId;
    private String buttonId;

}

To access the additional association table columns you will need to use JPQL, eg:

@Query("select eb.anotherAttribute, b.buttonAttrib from EntityButton eb JOIN eb.buttons b on eb.buttonId = b.buttonId where eb.entityId = :entityId")
发布评论

评论列表(0)

  1. 暂无评论