1

I work with a Spring boot app and have the following entities in the App,

@Entity
public class IpAddress {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "IP_ADDR_ID")
    private Long id;

    @Column(name = "IP_ADDRESS")
    @NotEmpty
    private String address;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "IP_ADDR_STATUS",
            joinColumns = {
                    @JoinColumn(name = "IP_ADDRESS_ID", referencedColumnName = "IP_ADDR_ID")
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "STATUS_ID", referencedColumnName = "S_ID")
            })
    private List<HttpInfoMessage> httpInfoMessages = new ArrayList<>();

    public IpAddress() {

    }

    public IpAddress(String address) {
        this.address = address;
    }

    public IpAddress(String address, List<HttpInfoMessage> httpInfoMessages) {
        this.address = address;
        this.httpInfoMessages = httpInfoMessages;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List<HttpInfoMessage> getHttpInfoMessages() {
        return httpInfoMessages;
    }

    public void setHttpInfoMessages(List<HttpInfoMessage> httpInfoMessages) {
        this.httpInfoMessages = httpInfoMessages;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof IpAddress)) return false;

        IpAddress ipAddress = (IpAddress) o;

        if (!getId().equals(ipAddress.getId())) return false;
        return getAddress().equals(ipAddress.getAddress());
    }

    @Override
    public int hashCode() {
        int result = getId().hashCode();
        result = 31 * result + getAddress().hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "IpAddress{" +
                "id=" + id +
                ", address='" + address + '\'' +
                '}';
    }
}

And, I have the second entity here,

@Entity
public class HttpInfoMessage {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "S_ID")
    private Long sId;

    @Column(name = "STATUS_ID")
    private Long statusId;

    @Column(name = "STATUS")
    @NotEmpty
    private String status;

    public HttpInfoMessage() {

    }

    public HttpInfoMessage(String status) {
        this.status = status;
    }

    public HttpInfoMessage(Long statusId, String status) {
        this.statusId = statusId;
        this.status = status;
    }

    public HttpInfoMessage(Long statusId, String status, List<IpAddress> ipAddresses) {
        this.statusId = statusId;
        this.status = status;
        this.ipAddresses = ipAddresses;
    }

    public Long getsId() {
        return sId;
    }

    public void setsId(Long sId) {
        this.sId = sId;
    }

    public Long getStatusId() {
        return statusId;
    }

    public void setStatusId(Long statusId) {
        this.statusId = statusId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @JsonIgnore
    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "httpInfoMessages")
    private List<IpAddress> ipAddresses = new ArrayList<>();

    public List<IpAddress> getIpAddresses() {
        return ipAddresses;
    }

    public void setIpAddresses(List<IpAddress> ipAddresses) {
        this.ipAddresses = ipAddresses;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof HttpInfoMessage)) return false;

        HttpInfoMessage httpInfoMessage1 = (HttpInfoMessage) o;

        if (!getStatusId().equals(httpInfoMessage1.getStatusId())) return false;
        return getStatus().equals(httpInfoMessage1.getStatus());
    }

    @Override
    public int hashCode() {
        int result = getStatusId().hashCode();
        result = 31 * result + getStatus().hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "Status{" +
                "statusId=" + statusId +
                ", status='" + status + '\'' +
                '}';
    }
}

When I persist a List in the database, It creates 3 tables,

The Ip Address table, namely ip_address,

enter image description here

The Http message info table, namely http_info_message,

enter image description here

And, the last table namely ip_addr_status maps the IP address ID with the Http Info Message ID,

enter image description here

I guess it good to show the formation of the IpAddress entity,

List<HttpInfoMessage> httpInfoMessages = new ArrayList<>();

for(int i=1;i<=10;i++){ 

    HttpInfoMessage httpInfoMessage = new HttpInfoMessage(404L, "FORBIDDEN_WEB_PAGE");
    httpInfoMessages.add(httpInfoMessage);
}

Then add the Http messages to the IpAddress,

IpAddress ip = new IpAddress("177.132.239.67", httpInfoMessages)

In the ip_addr_status table, I would like to add 3 more columns from the fields of address, statusId and status.

How to do that?

Arefe
  • 11,321
  • 18
  • 114
  • 168
  • If you want more columns in the table of `IpAddress` then you add fields to the class `IpAddress`! –  Oct 13 '17 at 07:42
  • 1
    You need to remove the `@ManyToMany` and create a join entity. https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns – Alan Hay Oct 13 '17 at 07:46
  • @DN1 I would like to add the columns in the last table as mentioned in the question – Arefe Oct 13 '17 at 07:55
  • 1
    you mean in the JOIN TABLE ? because your description is utterly incomprehensible. So then the element is persisted EMBEDDED into the JOIN TABLE ? so do that. Any JPA docs show how to persist a Collection with embedded elements –  Oct 13 '17 at 07:56
  • @DN1 I have updated the question and hope its clear now. Yes, any JPA docs provides the info and I have tried myself without success. This is the reason I come in the forum – Arefe Oct 13 '17 at 08:02
  • 1
    so post WHAT you have tried wrt embedding the collection element? http://www.datanucleus.org:15080/products/accessplatform_5_1/jpa/mapping.html#embedded_collection HttpInfoMessage would have to `@Embeddable` for starters ... –  Oct 13 '17 at 08:03
  • @AlanHay The docs make some sense, I will try again then – Arefe Oct 13 '17 at 08:04
  • 1
    Very good question, with all details and images. – Gaetano Piazzolla Oct 13 '17 at 08:15
  • @DN1 I will get back soon, thanks for the link – Arefe Oct 13 '17 at 08:15
  • @DN1 I could not solve the issue so far. Actually, I start a new question that you may want to look over: https://stackoverflow.com/questions/46740624/org-springframework-orm-jpa-jpasystemexception-could-not-set-field-value-post?noredirect=1#comment80428143_46740624 – Arefe Oct 14 '17 at 05:09
  • @AlanHay I start a new question: https://stackoverflow.com/questions/46740624/org-springframework-orm-jpa-jpasystemexception-could-not-set-field-value-post?noredirect=1#comment80428143_46740624 – Arefe Oct 14 '17 at 05:10

1 Answers1

1

If nothing else succeeds you can create this join table as separate entity. See accepted answer: Mapping many-to-many association table with extra column(s)

pirho
  • 11,565
  • 12
  • 43
  • 70
  • not solved so far. I start a new question over here: https://stackoverflow.com/questions/46740624/org-springframework-orm-jpa-jpasystemexception-could-not-set-field-value-post?noredirect=1#comment80428143_46740624 – Arefe Oct 14 '17 at 05:10