其他分享
首页 > 其他分享> > Hibernate:not-null属性引用一个null或瞬态值

Hibernate:not-null属性引用一个null或瞬态值

作者:互联网

问题解决了.谢谢.

现在,我有三个表movie_information,release_company和rating.
movie_information和评价具有一对一关系,而release_company和movie_information具有一对多关系.
我正在尝试使用休眠模式构建CMS,但是我发现我无法正确构建涉及这三个表的部分.

我的代码来了

MovieInformation.java

public class MovieInformation {
    private ReleaseCompany releaseCompany;
    private Rating rating;
    // Other data member
    // Constructor, getter and setter
    public ReleaseCompany getReleaseCompany(){
        ReleaseCompany newReleaseCompany = new ReleaseCompany(this.releaseCompany);
        return newReleaseCompany;
    }
    public Rating getRating(){
        Rating newRating = new Rating(this.rating);
        return newRating;
    }
    public void setReleaseCompany(ReleaseCompany releaseCompany){
        this.releaseCompany = new ReleaseCompany(releaseCompany);
    }
}

ReleaseCompany.java

public class ReleaseCompany {
    private int releaseCompanyId;
    private String releaseCompanyName;

    private Set<MovieInformation> movies = new HashSet<MovieInformation>();

    public ReleaseCompany(){
    }
    public ReleaseCompany(String releaseCompanyName){
        this.releaseCompanyName = releaseCompanyName;
    }
    public ReleaseCompany(ReleaseCompany releaseCompany){
        this.releaseCompanyName = releaseCompany.getReleaseCompanyName();
    }
    public Set<MovieInformation> getMovies() {  
        Set<MovieInformation> newMoviesSet = new HashSet<MovieInformation>(movies);
        return newMoviesSet;  
    }  
    public void setMovies(Set<MovieInformation> movies) {  
        this.movies = new HashSet<MovieInformation>(movies);
    }  
    // Other constructor, getter and setter
}

Rating.java

public class Rating {
    private MovieInformation movie;
    // other data member

    public void setMovie(MovieInformation movie){
        this.movie = new MovieInformation(movie);
    }
    // other constructor, getter and setter
}

MovieInformation.hbm.xml

<class name="cart.hibernate.movieInformation.MovieInformation" table="movie_information">
    <id column="move_id" name="movieId" type="long">
        <generator class="native"/>
    </id>

    <one-to-one name="rating" class="cart.hibernate.rating.Rating" cascade="save-update"></one-to-one> 

    <many-to-one name="releaseCompany" class="cart.hibernate.releaseCompany.ReleaseCompany"  unique="false"><!-- constrained="true" -->
        <column name="release_company_id" not-null="true" />
    </many-to-one>
    // Other property
</class>

Rating.hbm.xml

<class name="cart.hibernate.rating.Rating" table="rating">
  <id name="movieId" type="java.lang.Long">
      <column name="movie_id" />
      <generator class="foreign">
          <param name="property">movie</param>
      </generator>
  </id>

  <property column="avg_rate" name="avgRate" type="double"/>
  <property column="num_of_rate" name="numOfRate" type="long"/>

  <one-to-one name="movie" class="cart.hibernate.movieInformation.MovieInformation" constrained="true"></one-to-one>
</class>

ReleaseCompany.hbm.xml

<class name="cart.hibernate.releaseCompany.ReleaseCompany" table="release_company">
    <id column="release_company_id" name="releaseCompanyId" type="int">
        <generator class="native"/>
    </id>
    <property column="release_company_name" name="releaseCompanyName" type="string"/>

    <set name="movies" table="movie_information" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="movie_id" not-null="true" />
        </key>
        <one-to-many class="cart.hibernate.movieInformation.MovieInformation" />
    </set>
</class>

测试主要方法

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
tx = session.beginTransaction();

Rating rating = new Rating(5.6, 80);
ReleaseCompany releaseCompany = new ReleaseCompany();
releaseCompany.setReleaseCompanyName("a");
session.save(releaseCompany);        

//  ReleaseCompany releaseCompany = (ReleaseCompany)session.get(ReleaseCompany.class, 15); 
//  Another testing case that I have tried. It throw another exception,
//  if you need the information in this part, please let me know.

MovieInformation movie = new MovieInformation("Matrix", 150, 1, 50, date, releaseDate, "good description");
movie.setRating(rating);
movie.setReleaseCompany(releaseCompany);

rating.setMovie(movie);

session.save(movie);  // <--- Error occur here
tx.commit();

堆栈跟踪

org.hibernate.PropertyValueException: not-null property references a null or transient value: cart.hibernate.movieInformation.MovieInformation.releaseCompany
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:312)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
    at cart.hibernate.movieInformation.ManageMovieInformation.main(ManageMovieInformation.java:61)
BUILD SUCCESSFUL (total time: 1 second)

我读过几个类似的问题,有些人说releaseCompany(以我为例)对象尚未保存到数据库,因此setReleaseCompany()无法按预期工作.我不确定releaseCompany对象是否存在,因为我看不到MySql中的记录(我不确定如果抛出异常,是否将保存记录).而且,其中一些人说映射标记的属性不正确,但是我不确定我的是否正确.

我很抱歉在帖子中放了这么多代码,希望您能解决这个问题.

解决方法:

把事情简单化 :

public class MovieInformation {
    private ReleaseCompany releaseCompany;
    private Rating rating;
    // Other data member
    // Constructor, getter and setter
    public ReleaseCompany getReleaseCompany(){
        return releaseCompany;
    }
    public Rating getRating(){
        return rating;
    }
    public void setReleaseCompany(ReleaseCompany releaseCompany){
        this.releaseCompany = releaseCompany;
    }
}
public class ReleaseCompany {
    private int releaseCompanyId;
    private String releaseCompanyName;

    private Set<MovieInformation> movies = new HashSet<MovieInformation>();

    public ReleaseCompany(){
    }
    public ReleaseCompany(String releaseCompanyName){
        this.releaseCompanyName = releaseCompanyName;
    }

    public ReleaseCompany(ReleaseCompany releaseCompany){
        this.releaseCompanyName = releaseCompany.getReleaseCompanyName();
    }
    public Set<MovieInformation> getMovies() {  
        return movies;  
    }  
    private void setMovies(Set<MovieInformation> movies) {  
        this.movies = movies;
    }  
    // Other constructor, getter and setter
}
public class Rating {
    private MovieInformation movie;
    // other data member

    public void setMovie(MovieInformation movie){
        this.movie = movie;
    }
    // other constructor, getter and setter
}

请注意,setMovies(设置电影)是私有的,因此更改所有电影的唯一方法是执行以下操作:

releaseCompany.getMovies().clear();
releaseCompany.getMovies().addAll(newMovieCollection);

您需要这样做,因为:

>休眠需要跟踪对集合所做的更改
>休眠在集合周围放置代理以进行此跟踪.
>用另一个替换Set意味着休眠代理将丢失

标签:hibernate,hibernate-mapping,java
来源: https://codeday.me/bug/20191122/2059761.html