Home Advanced JPA with pivot table
Reply: 0

Advanced JPA with pivot table

user3315
1#
user3315 Published in June 24, 2018, 11:59 pm

I have an 5 entities, User, Roles, Permissions , UserRoles, PermisssionRoles, how would I construct a JPA class to fetch data using pivot tablesenter image description here

public class Roles implements Serializable {

// @OneToMany(mappedBy = "role_id") // private Collection permissionRoleCollection;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Size(max = 100)
@Column(name = "name")
private String name;
@Size(max = 50)
@Column(name = "code")
private String code;
@Size(max = 255)
@Column(name = "brief")
private String brief;
@Size(max = 8)
@Column(name = "status")
private String status;
@Basic(optional = false)
@NotNull
@Column(name = "date_created")
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(name = "date_updated")
@Temporal(TemporalType.TIMESTAMP)
private Date dateUpdated;

@JoinColumn(name = "created_by", referencedColumnName = "id")
@ManyToOne
private User createdBy;
@JoinColumn(name = "updated_by", referencedColumnName = "id")
@ManyToOne
private User updatedBy;

@ManyToOne
@JoinTable(name = "permission_role",
        joinColumns = @JoinColumn(name = "userid",
                referencedColumnName = "userid"),
        inverseJoinColumns = @JoinColumn(name = "groupid",
                referencedColumnName = "groupid")
)
private Collection<PermissionRole> permissionRoles;

public Roles() {
}

public Roles(Integer id) {
    this.id = id;
}

public Roles(Integer id, Date dateCreated) {
    this.id = id;
    this.dateCreated = dateCreated;
}

public Integer getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getBrief() {
    return brief;
}

public void setBrief(String brief) {
    this.brief = brief;
}

public String getStatus() {
    return status;
}

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

public Date getDateCreated() {
    return dateCreated;
}

public void setDateCreated(Date dateCreated) {
    this.dateCreated = dateCreated;
}

public Date getDateUpdated() {
    return dateUpdated;
}

public void setDateUpdated(Date dateUpdated) {
    this.dateUpdated = dateUpdated;
}


public User getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(User createdBy) {
    this.createdBy = createdBy;
}

public User getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(User updatedBy) {
    this.updatedBy = updatedBy;
}


@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Roles)) {
        return false;
    }
    Roles other = (Roles) object;
    return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
}

@Override
public String toString() {
    return "myproperty.v1.db._entities.Roles[ id=" + id + " ]";
}

}

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318463 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO