0

I had two class join with many to many relationship.

enter image description here

I use below code to do that

this is Member.class

    @Entity
    @Data
    @Table(name = "member")
    public class Member implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        
        @ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
        @JoinTable(joinColumns = {
                @JoinColumn(name = "member_id", referencedColumnName = "id", nullable = true) }, inverseJoinColumns = {
                        @JoinColumn(name = "event_info_id", referencedColumnName = "id", nullable = true) })
        private Set<EventInfo> eventInfo= new HashSet<>();
}

This is Eventinfo.class

@Entity
@Table(name = "event_info")
@Data
public class EventInfo implements Serializable{
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Column(name = "event_name")
    private String eventName;
    
    @Column(name = "event_date")
    @Temporal(TemporalType.DATE)
    private Date eventDate;
    
    @Column(name = "event_description")
    private String eventDescription;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "eventInfo")
    private Set<EventPic> eventPicSet;
    
    @JoinColumn(name = "event_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Event event;
    
}

Now I need to add more additional attribute to associate table. Is there any way to do that without using additional entity class.

I need only way that solve using this member.class and event_info.class

if there is no way why is that?

additional attribute is date

enter image description here

Community
  • 1
  • 1
wthamira
  • 2,032
  • 2
  • 21
  • 40
  • please tell us where in your JAVA model is this "additional attribute" –  Oct 30 '17 at 18:43
  • @DN1 I added new image. it contain member_event_info associate table. can I add this attribute without developing member_event_info.class – wthamira Oct 30 '17 at 18:53
  • I asked for the JAVA model. JPA persists what is in your JAVA CLASSES. If this attribute is not in your classes where do you expect it to magic the information from?!! –  Oct 30 '17 at 18:54
  • @DN I ask not magic. If it can or cant ? is there any wrong please guide me – wthamira Oct 30 '17 at 18:59
  • So you have no attribute then? So the answer is NO. –  Oct 30 '17 at 18:59
  • @Dn1 1 -can you understand this question. 1st image show the many-to-many relationship. that two java class provide solution for that. 2- now i add new attribute to associate entity call data. then i can use https://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns this link to solve that. but he go to additional class. is a way that solve without additional class – wthamira Oct 30 '17 at 19:09
  • We've already seen that you dont want to have an attribute in your java model, so no you cannot do that without an intermediate class (that has the attribute). So No. No. No. No. –  Oct 30 '17 at 20:19
  • You can use `@AssociationOverrides` but the code gets very cluttered and hard to understand and manage. Check an example [here](https://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/). – Abdullah Khan Nov 05 '17 at 13:34

0 Answers0