Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate JPA可联接共享ID_Java_Hibernate_Jpa - Fatal编程技术网

Java Hibernate JPA可联接共享ID

Java Hibernate JPA可联接共享ID,java,hibernate,jpa,Java,Hibernate,Jpa,我在将实体保存到数据库时遇到问题 @MappedSuperclass public abstract class BaseJpa { @Id @GeneratedValue(strategy= GenerationType.AUTO) private Integer id; public Integer getId() { return id; } public void setId(Integer id) {

我在将实体保存到数据库时遇到问题

@MappedSuperclass
public abstract class BaseJpa {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;

    public Integer getId() {
        return id;
    }

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

@Entity
@Table(name="genres")
public class GenreJpa extends BaseJpa{

    private String name;

    public String getName() {
        return name;
    }

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

@Entity
@Table(name="movies")
public class MovieJpa extends BaseJpa{

    @Type(type="text")
    private String name;

    private String releaseDate;

    @Type(type="text")
    private String summary;

    @JoinTable(name = "movie_genres", joinColumns = @JoinColumn(name = "movie_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "genre_id", referencedColumnName = "id"))
    @OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
    private List<GenreJpa> genres;

    private long votes;

    private double rank;

    public long getVotes() {
        return votes;
    }

    public void setVotes(long votes) {
        this.votes = votes;
    }

    public double getRank() {
        return rank;
    }

    public void setRank(double rank) {
        this.rank = rank;
    }

    public String getName() {
        return name;
    }

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

    public String getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(String releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public List<GenreJpa> getGenres() {
        return genres;
    }

    public void setGenres(List<GenreJpa> genres) {
        this.genres = genres;
    }
@MappedSuperclass
公共抽象类BaseJpa{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有整数id;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
}
@实体
@表(name=“genres”)
公共类GenreJpa扩展了BaseJpa{
私有字符串名称;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
@实体
@表(name=“movies”)
公共类电影JPA扩展了BaseJpa{
@类型(Type=“text”)
私有字符串名称;
私有字符串释放日期;
@类型(Type=“text”)
私有字符串摘要;
@JoinTable(name=“movie\u genres”,joinColumns=@JoinColumn(name=“movie\u id”,referencedColumnName=“id”),inverseJoinColumns=@JoinColumn(name=“genre\u id”,referencedColumnName=“id”))
@OneToMany(cascade=CascadeType.MERGE,fetch=FetchType.LAZY)
私人列表体裁;
私人长票;
私人双职级;
公众长期投票{
返回投票;
}
公众投票(长票){
这.投票=投票;
}
公共双getRank(){
返回等级;
}
公共无效设置秩(双秩){
这个.等级=等级;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getReleaseDate(){
返回发布日期;
}
public void setReleaseDate(字符串releaseDate){
this.releaseDate=releaseDate;
}
公共字符串getSummary(){
返回摘要;
}
公共void集合摘要(字符串摘要){
this.summary=摘要;
}
公共列表getGenres(){
回归体裁;
}
公共类型(列表类型){
this.genres=流派;
}
有了这些实体,我进行了两次迭代:

  • 我保存了一个类型列表(34个对象)。检查我的数据库表,我可以获取所有34个类型
  • 我保存了一个电影列表(超过1百万个对象)。现在每个电影对象都有一个类型列表,并且这些类型正以不同的ID插入到类型表中。是否无法使其共享已插入的类型?是否不再插入类型
  • 用于插入数据的方法:

    public void batchInsertMovies(List<MovieJpa> movies){
        EntityManager entityManager = EMF.get().createEntityManager();
        try{
            entityManager.getTransaction().begin();
            int i = 0;
            for(MovieJpa movie : movies){
                entityManager.persist(movie);
                i++;
                if(i == 30){
                    //flush a batch of inserts and release memory
                    entityManager.flush();
                    entityManager.clear();
                }
            }
            entityManager.getTransaction().commit();
        } catch (Exception e){
            e.printStackTrace();
            entityManager.getTransaction().rollback();
        } finally {
            entityManager.close();
        }
    }
    
    public void batchInsertGenres(List<GenreJpa> genres){
        EntityManager entityManager = EMF.get().createEntityManager();
        try{
            entityManager.getTransaction().begin();
            int i = 0;
            for(GenreJpa genre : genres){
                entityManager.persist(genre);
                i++;
                if(i == 5){
                    //flush a batch of inserts and release memory
                    entityManager.flush();
                    entityManager.clear();
                }
            }
            entityManager.getTransaction().commit();
        } catch (Exception e){
            e.printStackTrace();
            entityManager.getTransaction().rollback();
        } finally {
            entityManager.close();
        }
    }
    
    public void batchInsertMovies(列出电影){
    EntityManager EntityManager=EMF.get().createEntityManager();
    试一试{
    entityManager.getTransaction().begin();
    int i=0;
    电影(JPA电影:电影){
    entityManager.persist(电影);
    i++;
    如果(i==30){
    //刷新一批插入并释放内存
    entityManager.flush();
    entityManager.clear();
    }
    }
    entityManager.getTransaction().commit();
    }捕获(例外e){
    e、 printStackTrace();
    entityManager.getTransaction().rollback();
    }最后{
    entityManager.close();
    }
    }
    公共类型(列表类型){
    EntityManager EntityManager=EMF.get().createEntityManager();
    试一试{
    entityManager.getTransaction().begin();
    int i=0;
    for(体裁:体裁){
    entityManager.persist(类型);
    i++;
    如果(i==5){
    //刷新一批插入并释放内存
    entityManager.flush();
    entityManager.clear();
    }
    }
    entityManager.getTransaction().commit();
    }捕获(例外e){
    e、 printStackTrace();
    entityManager.getTransaction().rollback();
    }最后{
    entityManager.close();
    }
    }
    

    电影包含流派。此集合中的每个流派实例都应该填充id字段,否则Hibernate会认为它是一个新对象,如果级联选项合适,则会尝试将其持久化。

    为什么有一个多个联接表?它不应该是多个吗?你是正确的,它应该是多个。请对你的评论进行投票。
    entityManager.persist(movie);