开发者

Using JPA mapped by not primary key field?

开发者 https://www.devze.com 2023-01-14 14:39 出处:网络
I am having troubles getting this working and I wonder if what I am doing simply does not make sense?

I am having troubles getting this working and I wonder if what I am doing simply does not make sense?

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;
}

The problem I am having is that I am able to add parts, the database looks correct, then when I attempt to fetch the the parts list I get an empty list.

I can get it to work if I compromise on the database structure by changing these classes:

// TestSample contains a list that is mapped not by the primary key of the Sample
public class TestSample extends Sample {
    @OneToMany(开发者_如何学PythonmappedBy="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;
}

The tables are being auto generated by hibernate, so they are coming out like this:

application
  id : number
  ....

test_sample
  id : number
  application_id : number
  ...

part
  id : number
  application_id : number

If I change it to the less desirable way that works, the last table is different:

part
  id : number
  test_sample_id : number

Because the id's in all cases are being auto generated, there are no shared primary keys. Essentially what I am trying to do is use mappedby where mappedby is referring to a field that is not the primary key of the table/class called "TestSample". This is what I am not sure if makes sense in JPA.


The OneToMany is bi-directional with the "Part" class. I think this is getting very difficult to explain (:

Your one-to-many association between TestSample and Part is not bidirectional, the mappedBy is not correct (the application table is not owning the relation, it is not even aware of test_sample), your mapping doesn't make sense. There is something to change.

I think that you should show what the expected tables are, not the generated one (since the mappings are incoherent, the generated result can't be satisfying). You are talking about compromise so I believe that you have an idea of what the expected result should be. Please show it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号