Home Why my foreign key is null when saving to h2 db, using JPA
Reply: 0

Why my foreign key is null when saving to h2 db, using JPA

user1570
1#
user1570 Published in April 26, 2018, 7:55 am

What is the difference between these 2 codes. The 1st one shows null on my foreign key which is individualId. The 2nd one is not. Why?

//1st code:

@Entity
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Individual {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "INDIVIDUAL_ID")
    private Long individualId;

    @OneToMany(mappedBy="individual",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Identification> identifications = new ArrayList<Identification>();
}

@Entity
public class Identification {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "IDT_ID")
    private Long id;

    @ManyToOne
    @JoinColumn(name="individualId")
    private Individual individual;

//second code

//replaced @OneToMany in the first code & then i just dont add @ManyToOne in the Identification Class and it works fine. Why?

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "INDIVIDUAL_ID", referencedColumnName = "INDIVIDUAL_ID")
private List<Identification> identifications = new ArrayList<Identification>();

When i search for JPA tutorial in google the 1st code is the one that i always read. declare @OneToMany in the parent class and add mappedBy, declare @ManyToOne in the child class. But why the 2nd code works perfect than the 1st code? it just let me declare @OneToMany only in the parent class ?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.335111 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO