admin管理员组文章数量:1208155
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
1 Answer
Reset to default 0I 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")
本文标签: springHow to add Columns from Junction table to the ChildStack Overflow
版权声明:本文标题:spring - How to add Columns from Junction table to the Child - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738697110a2107444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论