复合主键JPA



import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


@Embeddable
public class RoleAuthorityId  implements Serializable {

/*@Column(length = 3)
private String roleId;
@Column(length = 3)
private String authorityId;*/

@ManyToOne  
@JoinColumn(name = "role_id", nullable = false)  
private Role role; 

@ManyToOne  
@JoinColumn(name = "authority_id", nullable = false)  
private Authority authority;

/*public String getRoleId() {
return roleId;
}

public void setRoleId(String roleId) {
this.roleId = roleId;
}

public String getAuthorityId() {
return authorityId;
}

public void setAuthorityId(String authorityId) {
this.authorityId = authorityId;
}*/

/*@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((authorityId == null) ? 0 : authorityId.hashCode());
result = prime * result
+ ((roleId == null) ? 0 : roleId.hashCode());
return result;
}*/

/*@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AirLinePK other = (AirLinePK) obj;
if (authorityId == null) {
if (other.authorityId != null)
return false;
} else if (!authorityId.equals(other.authorityId))
return false;
if (roleId == null) {
if (other.roleId != null)
return false;
} else if (!roleId.equals(other.roleId))
return false;
return true;
}*/
}

----------------------------------------------------------------------------------------------------------------------------------------------------

@Entity(name="CITY_ZHTT")
public class RoleAuthority {
    @EmbeddedId
    //这个注解用于标注id这个属性为实体的标识符,因为我们使用的是复合主键类,所以我们要用@EmbeddedId这个专门针对复合主键类的标志实体标识符的注解。
    private RoleAuthorityId id;
    @Column(length=20)
    public RoleAuthorityId  getId() {
        return id;
    }
    public void setId(RoleAuthorityId  id) {
        this.id = id;
    }
}

你可能感兴趣的:(jpa,复合主键,多对一)