如何在JPA实体中添加@OneToMany关系后更新mysql表?

时间:2017-11-11 16:14:25

标签: java mysql spring jpa

我有一个使用mysql数据库的spring / jpa项目。我已经更新了一个已经存在的实体,因此它与我创建的新表有一个新的@OneToMany关系(其中有一个@ManyToOne annoation)。我已经使用嵌入式数据库进行单元测试,并且工作正常。但是当我尝试将它部署到我的开发服务器时,似乎没有注册映射,并且数据只返回一个大小为0的列表,它应该返回更多。

我打电话的功能是

public List<Recommendation> getRecommendationsByEmployeeId()

我相信这个问题与创建数据库时所做的一些JPA魔法有关,当针对已经存在的数据库进行部署时不会发生。这是正确的假设吗?或者为什么在针对嵌入式数据库运行我的测试时这会起作用,而不是针对已经存在的开发数据库?

这些是相关实体。

    @Entity
    @Table(name = "recommendation")
    public class Recommendation extends BusinessEntity {

        @ManyToOne
        @JoinColumn(name="employeeid")
        @JsonBackReference
        //This is simply to avoid a stackoverflow error 
        private Employee employee;

//New relationship
        @OneToMany(mappedBy = "recommendation", fetch = FetchType.EAGER,  cascade = CascadeType.ALL)
        @JsonBackReference
        List<RecommendationImage> recommendationimages;

        public Recommendation(){}

        public Recommendation(Employee employee, String title, String description, double targetLat, double targetLong,
                              String street, String city, String country
                              ) {
            this.employee = employee;
            this.title = title;
            this.description = description;
            this.targetLat = targetLat;
            this.targetLong = targetLong;
            this.street = street;
            this.city = city;
            this.country = country;
            this.active = true;
        }


        public Employee getEmployee() {
            return employee;
        }

        public void setEmployee(Employee employee) {
            this.employee = employee;
        }

        public List<RecommendationImage> getRecommendationimages() {
            return recommendationimages;
        }

        public void setRecommendationimages(List<RecommendationImage> recommendationimages) {
            this.recommendationimages = recommendationimages;
        }
    }

新实体

@Entity
@Table(name = "recommendationimage")
public class RecommendationImage extends ImageEntity{

        @ManyToOne
        @JoinColumn(name="recommendationid")
        private Recommendation recommendation;

        public RecommendationImage(){}

        public RecommendationImage(Recommendation recommendation, String path){
            this.recommendation = recommendation;
            this.path = path;
        }

        public Recommendation getRecommendation() {
            return recommendation;
        }

        public void setRecommendation(Recommendation recommendation) {
            this.recommendation = recommendation;
        }




    }

对此问题的任何帮助都会很棒!谢谢!

0 个答案:

没有答案