JPA 无法填写联接 table 中的条目

JPA cannot fill entries in join table

我正在尝试映射 class 问题和 class 域之间的双向多对多关系。因此,持久性单元在数据库中创建了一个连接 table,但似乎没有条目在数据库中弹出。

这是一些代码:

class 问题

package domain;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import static javax.persistence.GenerationType.SEQUENCE;
import javax.xml.bind.annotation.XmlRootElement;


@Entity
@XmlRootElement
public class Problem implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private User user;
    private String description;
    private int maxprice;
    private int priority;
    private Solution solution;
    private Location location;
    private List<Domain> domains;

    @Id
    //@GeneratedValue(strategy = GenerationType.AUTO)
    @SequenceGenerator(name="User_Seq", allocationSize=25)
    @GeneratedValue(strategy=SEQUENCE, generator="Problem_Seq")
    public Long getId() {
        return id;
    }

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

    @ManyToOne
    //@JoinColumn(name="user_id")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getMaxPrice() {
        return maxprice;
    }

    public void setMaxPrice(int maxprice) {
        this.maxprice = maxprice;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }
 
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="solution_id")
    public Solution getSolution() {
        return solution;
    }

    public void setSolution(Solution solution) {
        this.solution = solution;
    }

    @ManyToOne
    @JoinColumn(name="location_id")
    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    @ManyToMany
    @JoinTable(name="problem_domain",
            joinColumns={@JoinColumn(name="problem_id", referencedColumnName="ID")},
            inverseJoinColumns={@JoinColumn(name="domain_id", referencedColumnName="ID")})
    public List<Domain> getDomains() {
        return domains;
    }

    public void setDomains(List<Domain> domains) {
        this.domains = domains;
    }
    
    public void addDomain(Domain domain){
        //this.domains.add(domain); //Throws NullpointerException om een of andere reden.
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Problem)) {
            return false;
        }
        Problem other = (Problem) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "domain.Problem[ id=" + id + " ]";
    }
    
}

class 域

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package domain;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import static javax.persistence.GenerationType.SEQUENCE;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement
public class Domain implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private List<Problem> problems;
    private List<Domain> subDomains;
    private Domain superDomain;

    @Id
    @SequenceGenerator(name="Dom_Seq", allocationSize=25)
    @GeneratedValue(strategy=SEQUENCE, generator="Dom_Seq")
    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @ManyToMany(mappedBy = "domains")    
    public List<Problem> getProblems() {
        return problems;
    }

    public void setProblems(List<Problem> problems) {
        this.problems = problems;
    }

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="superdomain_id")
    public List<Domain> getSubDomains() {
        return subDomains;
    }

    public void setSubDomains(List<Domain> subDomains) {
        this.subDomains = subDomains;
    }

    public Domain getSuperDomain() {
        return superDomain;
    }

    public void setSuperDomain(Domain superDomain) {
        this.superDomain = superDomain;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Domain)) {
            return false;
        }
        Domain other = (Domain) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "domain.Domain[ id=" + id + " ]";
    }
    
}

我们向数据库添加问题和域的代码

Problem problem = new Problem();

Domain domain = new Domain();
domain.setName(domainString);

domainFacade.create(domain);
        
problemFacade.create(problem);
        
problem.addDomain(domain);
        
problemFacade.edit(problem);

以及对 DB 的一些直观解释

您尝试将域列表保存到问题中吗?

代码示例: 问题problem = new Problem();

Domain domain = new Domain();
domain.setName(domainString);
domainFacade.create(domain);

List<Domain> domains = new ArrayList<Domain>();
domains.add(domain);


problem.setDomains(domains);
problemFacade.create(problem);

正如@Neil Stockton 和其他人所说,我的问题的答案是我必须有一个简单地将对象添加到列表中的 addMethod。