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

admin2025-04-23  6

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 Apr 2 at 16:02 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Jan 20 at 12:46 ivoronlineivoronline 1,1052 gold badges11 silver badges25 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")
转载请注明原文地址:http://anycun.com/QandA/1745369120a90639.html