ManyToMany关系的空连接表

时间:2016-08-31 22:13:08

标签: java hibernate jpa

以前已经回答了一个类似的问题和另一个问题。我使用Hibernate建立了双向@ManyToMany关系。数据持久存储到单独的实体表中,但映射到实体表的id列为空。我在Empty Join Table resulting from JPA ManyToManyManyToMany relationship, deleting records from relationship tableHibernate Many-to-many association: left hand side collection contains elements, but right hand side collection is empty中尝试了这些建议,但到目前为止还没有取得成功。下面是我的完整性代码(我还在学习java和spring框架,所以我提前为任何菜鸟错误道歉):

CaseStudy Class

 @Entity
public class CaseStudy {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    private Long caseStudyId;
    private String location;
    private String equipment;
    private String issue;
    private String solution;
    private String benefit;
    private float upper_dissolved_Oxygen;
    private float lower_dissolved_Oxygen;
    private float upper_pH;
    private float lower_pH;
    private float upper_temp;
    private float lower_temp;
    private float upper_conductivity;
    private float lower_conductivity;

    @CreationTimestamp
    private Date created;

    @ManyToOne
    @JsonBackReference
    private User user;

    private int likes;

    @ManyToMany(mappedBy="casestudyList", fetch = FetchType.EAGER)
    private List<WaterQuality> waterQualityList  = new List<WaterQuality>();

    @OneToMany(mappedBy = "casestudy", fetch = FetchType.EAGER)
    private List<Comment> commentList;

    public Long getCaseStudyId() {
        return caseStudyId;
    }


    public void setCaseStudyId(Long caseStudyId) {
        this.caseStudyId = caseStudyId;
    }


    public String getLocation() {
        return location;
    }


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


    public String getIssue() {
        return issue;
    }


    public void setIssue(String issue) {
        this.issue = issue;
    }


    public String getSolution() {
        return solution;
    }


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


    public String getBenefit() {
        return benefit;
    }


    public void setBenefit(String benefit) {
        this.benefit = benefit;
    }


    public Date getCreated() {
        return created;
    }


    public void setCreated(Date created) {
        this.created = created;
    }


    public User getUser() {
        return user;
    }


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


    public int getLikes() {
        return likes;
    }


    public void setLikes(int likes) {
        this.likes = likes;
    }


    public List<Comment> getCommentList() {
        return commentList;
    }


    public void setCommentList(List<Comment> commentList) {
        this.commentList = commentList;
    }


    public String getEquipment() {
        return equipment;
    }


    public void setEquipment(String equipment) {
        this.equipment = equipment;
    }


    public float getUpper_dissolved_Oxygen() {
        return upper_dissolved_Oxygen;
    }


    public void setUpper_dissolved_Oxygen(float upper_dissolved_Oxygen) {
        this.upper_dissolved_Oxygen = upper_dissolved_Oxygen;
    }


    public float getLower_dissolved_Oxygen() {
        return lower_dissolved_Oxygen;
    }


    public void setLower_dissolved_Oxygen(float lower_dissolved_Oxygen) {
        this.lower_dissolved_Oxygen = lower_dissolved_Oxygen;
    }


    public float getUpper_pH() {
        return upper_pH;
    }


    public void setUpper_pH(float upper_pH) {
        this.upper_pH = upper_pH;
    }


    public float getLower_pH() {
        return lower_pH;
    }


    public void setLower_pH(float lower_pH) {
        this.lower_pH = lower_pH;
    }


    public float getUpper_temp() {
        return upper_temp;
    }


    public void setUpper_temp(float upper_temp) {
        this.upper_temp = upper_temp;
    }


    public float getLower_temp() {
        return lower_temp;
    }


    public void setLower_temp(float lower_temp) {
        this.lower_temp = lower_temp;
    }


    public float getUpper_conductivity() {
        return upper_conductivity;
    }


    public void setUpper_conductivity(float upper_conductivity) {
        this.upper_conductivity = upper_conductivity;
    }


    public float getLower_conductivity() {
        return lower_conductivity;
    }


    public void setLower_conductivity(float lower_conductivity) {
        this.lower_conductivity = lower_conductivity;
    }

    public List<WaterQuality> getWaterQualityList() {
        return waterQualityList;
    }


    public void setWaterQualityList(List<WaterQuality> waterQualityList) {
        this.waterQualityList = waterQualityList;
    }


}

WaterQuality Class

    @Entity
public class WaterQuality {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long waterQualityId;
    private String description;
    private String status;
    private float dissolved_Oxygen;
    private float pH;
    private float temp;
    private float conductivity;

    @CreationTimestamp
    private Date publishDate;

    @ManyToOne
    @JsonBackReference
    private User user;

    @ManyToMany(targetEntity = CaseStudy.class, cascade = {CascadeType.ALL})
    @JoinTable(
               joinColumns = {@JoinColumn(name="water_quality_id")},
               inverseJoinColumns = {@JoinColumn(name="case_study_id")})
    private Set<CaseStudy> casestudyList = new HashSet<CaseStudy>();

    public Long getWaterQualityId() {
        return waterQualityId;
    }

    public void setWaterQualityId(Long waterQualityId) {
        this.waterQualityId = waterQualityId;
    }

    public String getDescription() {
        return description;
    }

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

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public float getDissolved_Oxygen() {
        return dissolved_Oxygen;
    }

    public void setDissolved_Oxygen(float dissolved_Oxygen) {
        this.dissolved_Oxygen = dissolved_Oxygen;
    }

    public float getpH() {
        return pH;
    }

    public void setpH(float pH) {
        this.pH = pH;
    }

    public float getTemp() {
        return temp;
    }

    public void setTemp(float temp) {
        this.temp = temp;
    }

    public float getConductivity() {
        return conductivity;
    }

    public void setConductivity(float conductivity) {
        this.conductivity = conductivity;
    }

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    public User getUser() {
        return user;
    }

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

    public Set<CaseStudy> getCasestudyList() {
        return casestudyList;
    }

    public void setCasestudyList(Set<CaseStudy> casestudyList) {
        this.casestudyList = casestudyList;
    }

    public List<CaseStudy> getDissolvedOxygenalert() {
        List<CaseStudy> dissolvedOxygenalert = casestudyList.stream()           //convert list to stream
                .filter(casestudy -> casestudy.getUpper_dissolved_Oxygen() < getDissolved_Oxygen() ||  casestudy.getLower_dissolved_Oxygen() > getDissolved_Oxygen())
                .collect(Collectors.toList());          //collect the output and convert streams to a List
        System.out.print(dissolvedOxygenalert);
        return dissolvedOxygenalert;
    }


}

我已经尝试删除数据库并再次创建它,但到目前为止还没有解决问题。

以下是将数据保存到数据库的代码 ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++

CaseStudy服务实施

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scrunch.data.dao.CaseStudyDao;
import com.scrunch.data.model.CaseStudy;
import com.scrunch.data.model.User;
import com.scrunch.data.service.CaseStudyService;

@Service
public class CaseStudyServiceImpl implements CaseStudyService{

@Autowired
private CaseStudyDao casestudyDao;

public CaseStudy save(CaseStudy casestudy) {
    return casestudyDao.save(casestudy);
}

public Set<CaseStudy> findByUser(User user) {
    return casestudyDao.findByUser(user);
}

public CaseStudy findByCaseStudyId(Long caseStudyId) {
    return casestudyDao.findByCaseStudyId(caseStudyId);
}

public Set<CaseStudy> findAll() {
    return casestudyDao.findAll();
}
}

WaterQuality服务实施

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scrunch.data.dao.WaterQualityDao;
import com.scrunch.data.model.User;
import com.scrunch.data.model.WaterQuality;
import com.scrunch.data.service.WaterQualityService;

@Service
public class WaterQualityServiceImpl implements WaterQualityService{

//  private static final Logger LOGGER =     LoggerFactory.getLogger(WaterQualityServiceImpl.class);

@Autowired
private WaterQualityDao waterQualityDao;


@Override
public List<WaterQuality> listAllWaterQualityByUserAndDescription(
        User user, String description) {
    return waterQualityDao.findAllWaterQualityByUserAndDescriptionContaining(user, description);
}

@Override
public WaterQuality save(WaterQuality waterQuality) {
    return waterQualityDao.save(waterQuality);
}

@Override
public List<WaterQuality> findByUser(User user) {
    return waterQualityDao.findByUser(user);
}

@Override
public WaterQuality findWaterQualityById(Long waterQualityId) {
    return waterQualityDao.findByWaterQualityId(waterQualityId);
}

@Override
public List<WaterQuality> findAll() {
    return waterQualityDao.findAll();
}
}

WaterQuality Controller

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.scrunch.data.model.CaseStudy;
import com.scrunch.data.model.User;
import com.scrunch.data.model.WaterQuality;
import com.scrunch.data.service.WaterQualityService;

@RestController
@RequestMapping("/rest")
public class WaterQualityResource {
private String location;
private String equipment;
private String issue;
private String solution;
private String benefit;
private float upper_dissolved_Oxygen;
private float lower_dissolved_Oxygen;
private int likes;



@Autowired
private WaterQualityService waterQualityService;

@RequestMapping(value="/waterquality/add", method=RequestMethod.POST)

public WaterQuality addWaterQuality(@RequestBody WaterQuality waterQuality) {

    waterQuality.setCasestudyList(new HashSet<CaseStudy>());

    CaseStudy casestudy = new CaseStudy();
    casestudy.setLocation(location);
    casestudy.setEquipment(equipment);
    casestudy.setIssue(issue);
    casestudy.setSolution(solution);
    casestudy.setBenefit(benefit);
    casestudy.setUpper_dissolved_Oxygen(upper_dissolved_Oxygen);
    casestudy.setLower_dissolved_Oxygen(lower_dissolved_Oxygen);


    waterQuality.getCasestudyList().add(casestudy);
    return waterQualityService.save(waterQuality);
}

获取所有水质响应返回的请求以及相同结果的无限循环。

     [{"waterQualityId":1,"description":"sdfdsf","status":"dsfdsf","dissolved_Oxygen":5.0,"pH":7.0,"temp":72.0,"conductivity":500.0,"publishDate":1472784062000,"dissolvedOxygenalert":[{"caseStudyId":2,"location":null,"equipment":null,"issue":null,"solution":null,"benefit":null,"upper_dissolved_Oxygen":0.0,"lower_dissolved_Oxygen":0.0,"upper_pH":0.0,"lower_pH":0.0,"upper_temp":0.0,"lower_temp":0.0,"upper_conductivity":0.0,"lower_conductivity":0.0,"created":1472784062000,"likes":0,"waterQualityList":[{"waterQualityId":1,"description":"sdfdsf","status":"dsfdsf","dissolved_Oxygen":5.0,"pH":7.0,"temp":72.0,"conductivity":500.0,"publishDate":1472784062000,"dissolvedOxygenalert":[{"caseStudyId":2,"location":null,"equipment":null,"issue":null,"solution":null,"benefit":null,"upper_dissolved_Oxygen":0.0,"lower_dissolved_Oxygen":0.0,"upper_pH":0.0,"lower_pH":0.0,"upper_temp":0.0,"lower_temp":0.0,"upper_conductivity":0.0,"lower_conductivity":0.0,"created":1472784062000,"likes":0,"waterQualityList":

我能够将数据填充到连接表中,但它似乎无休止地填充。它会在添加水质数据时发生。

1 个答案:

答案 0 :(得分:1)

在发布的代码中不清楚您是如何尝试保存关联实体的。你应该坚持这样......

WaterQuality waterQuality = new WaterQuality();
//Set other fields too

CaseStudy caseStudy1 = new CaseStudy();
CaseStudy caseStudy2 = new CaseStudy();
CaseStudy caseStudy3 = new CaseStudy();
CaseStudy caseStudy4 = new CaseStudy();
//Set other fileds of case study too

List<CaseStudy> caseStudyList = waterQuality.getCaseStudyList();
caseStudyList.add(caseStudy1);
caseStudyList.add(caseStudy2);
caseStudyList.add(caseStudy3);
caseStudyList.add(caseStudy4);

waterQualityService.save(waterQuality);

如果您将WaterQuality对象分配给CaseStudy实体并保存CaseStudy,则不会在JoinTable中填充关联。

还有一个重要的事情,使用Set而不是List for Collection。如果是List,假设,你已经有100个关于waterQuality的案例研究,如果你想删除其中一个,那么它将首先删除整个案例研究然后再次添加所有案例研究,除了已删除的案例。如果您的集合不包含重复项,请使用Set。如果您的集合也包含重复项,那么请使用List with index。