我很难让这个工作起来,我想知道我所做的只是没有意义吗?
public class Application {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private long id;
....
}
@MappedSuperclass
public abstract class Sample {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne (cascade=CascadeType.ALL)
protected Application application;
....
}
// TestSample contains a list that is mapped not by the primary key of the Sample
public class TestSample extends Sample {
@OneToMany(mappedBy="application", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Part> parts = new ArrayList<Part>();
....
}
public class Part {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id = 0;
@ManyToOne (cascade=CascadeType.ALL, fetch=FetchType.LAZY)
Application application;
}
parts
我得到一张空名单。
如果我通过更改这些类来破坏数据库结构,则可以使其正常工作:
// TestSample contains a list that is mapped not by the primary key of the Sample
public class TestSample extends Sample {
@OneToMany(mappedBy="testSample", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Part> parts = new ArrayList<Part>();
....
}
public class Part {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id = 0;
@ManyToOne (cascade=CascadeType.ALL, fetch=FetchType.LAZY)
TestSample testSample;
}
application
id : number
....
test_sample
id : number
application_id : number
...
part
id : number
application_id : number
如果我将其更改为不太理想的工作方式,则最后一个表会有所不同:
part
id : number
test_sample_id : number
因为id在所有情况下都是自动生成的,所以没有共享的主键。实际上,我要做的是使用mappedby,其中mappedby引用的字段不是名为“TestSample”的表/类的主键。我不确定这在JPA中是否有意义。