Hi,
I have three tables: units, units_authorities and authorities. The last one is irrelevant; units is a join table, so it has foreign keys for both units and authorities. It cointans additional columns.
Now what I want to do is to create a collection of units_authorities in units. I have tried the code below:
Code:
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="units")
public class Unit{
private Integer id;
private List<UnitAuthority> unitAuthorities;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id")
public Integer getId(){
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToMany(mappedBy="unit" fetch=FetchType.EAGER)
public List<UnitAuthority> getUnitAuthorities() {
return unitAuthorities;
}
public void setUnitAuthorities(List<UnitAuthority> unitAuthorities) {
this.unitAuthorities = unitAuthorities;
}
}
Code:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity @Table(name="units_authorities")
public class UnitAuthority implements Serializable{
private static final long serialVersionUID = 1L;
private Unit unit;
private String authority;
private Integer usersNumber;
@Id
@OneToOne
@JoinColumn(name="fk_units_id")
public Unit getUnit() {
return unit;
}
public void setUnit(Unit unit) {
this.unit = unit;
}
@Id
@Column(name="fk_dic_authorities_id")
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
@Column(name="users_number")
public Integer getUsersNumber() {
return usersNumber;
}
public void setUsersNumber(Integer usersNumber) {
this.usersNumber = usersNumber;
}
}
But it gets me "mappedby reference an unknown target entity property" error. I already know that the problem is caused by @Id on unit in UnitAuthority. How to get around this?