Arefe
Arefe

Reputation: 12431

Map additional field in the Java Persistence

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?

Upvotes: 1

Views: 183

Answers (1)

pirho
pirho

Reputation: 12215

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)

Upvotes: 1

Related Questions