我希望我的ERD关系对这种情况有好处。
在这里,到目前为止我做了什么?随后:Giannigar’s blog
执行此类映射的最佳方法是什么?这些是我的实体类
@Entity
@Table(name = "t")
public class T
{
//… other fields
@OneToMany(mappedBy = "t", targetEntity = TMJoin.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonManagedReference
private List<TMJoin> tmJoins = new ArrayList<TMJoin>();
//… setter/getter methods
}
@Entity
@Table(name = "m")
public class M
{
//… other fields
@OneToMany(mappedBy = "m", targetEntity = TMJoin.class)
@JsonBackReference
private List<TMJoin> tmJoins = new ArrayList<TMJoin>();
//… setter/getter methods
}
@Entity
@Table(name = "u")
public class U {
//… other fields
@OneToMany(mappedBy = "u", targetEntity = TMJoin.class)
@JsonBackReference
private List<TMJoin> tmJoins = new ArrayList<TMJoin>();
//… setter/getter methods
}
@Entity
@Table(name= "t_m_join")
public class TMJoin
{
@Id
@SequenceGenerator(name = "tMJoinSeq", sequenceName = "seq_t_m_join_id", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "tMJoinSeq")
private long id;
@ManyToOne(targetEntity=T.class, fetch=FetchType.LAZY)
@JoinColumns({ @JoinColumn(name="t_id", referencedColumnName="id", nullable=false) })
@JsonBackReference
private T t;
@ManyToOne(targetEntity=U.class, fetch=FetchType.EAGER)
@JoinColumns({ @JoinColumn(name="u_id", referencedColumnName="id", nullable=false) })
@JsonManagedReference
private U u;
@ManyToOne(targetEntity=M.class, fetch=FetchType.EAGER)
@JoinColumns({ @JoinColumn(name="m_id", referencedColumnName="id", nullable=false) })
@JsonManagedReference
@JsonProperty("m")
private M m;
//… setter/getter methods
}
也许有更好的方法可以使用额外的参考列进行此类连接表映射?
编辑: 我在保存T对象时遇到错误。
web_1 | ERROR [2016-04-13 15:54:09,619] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: 2270a7a8addbc3e7
web_1 | ! java.lang.IllegalArgumentException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.core.TMJoin)