简体   繁体   中英

JPA + hibernate foreign key is null

I have 2 tables in my database. I have got a Project which can have multiple builds. One build belongs to one project. Everything works fine, except the foreign key in my build table remains null.

Project

@Entity(name="project")
public class Project implements Serializable {

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

@Column
@JsonProperty("displayName")
private String name;

@JsonProperty("builds")
@JsonIgnore
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = ("project"))
private Collection<Build> builds;

public Project() {
}

public Project(String name) {
    this.name = name;
}

public Collection<Build> getBuilds() {
    return builds;
}

public void setBuilds(Collection<Build> builds) {
    this.builds = builds;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}
}

Build

@Entity(name = "build")
public class Build implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
@JsonProperty("number")
private Integer number;

@Column
@JsonProperty("url")
private String url;

@JsonBackReference
@ManyToOne
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "project")
private Project project;

public Build() {
}

public Build(String url, Project project, Integer number) {
    this.url = url;
    this.project = project;
    this.number = number;
}

public Long getId() {
    return id;
}

public Integer getNumber() {
    return number;
}

public String getUrl() {
    return url;
}

public void setId(Long id) {
    this.id = id;
}

public void setNumber(Integer number) {
    this.number = number;
}

public void setUrl(String url) {
    this.url = url;
}

public Project getProject() {
    return project;
}

public void setProject(Project project) {
    this.project = project;
}
}

Does anyone see the problem?

Using JPA you need explicitly define the Project in your Build object.

Project project = new Project();

Build build = new Build();
build.setProject(project);

project.setBuilds(Collections.singletonList(build));

// now you can persist it    
em.persist(project)

From Hibernate documentation :

First, keep in mind that Hibernate does not affect normal Java semantics. How did we create a link between a Person and an Event in the unidirectional example? You add an instance of Event to the collection of event references, of an instance of Person. If you want to make this link bi-directional, you have to do the same on the other side by adding a Person reference to the collection in an Event. This process of "setting the link on both sides" is absolutely necessary with bi-directional links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM