Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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 为什么外键的值总是零?_Java_Hibernate_Orm - Fatal编程技术网

Java 为什么外键的值总是零?

Java 为什么外键的值总是零?,java,hibernate,orm,Java,Hibernate,Orm,我尝试使用hibernate构建一些持久性应用程序(我的第一个)。该应用程序具有类型和书籍。体裁课有一套书。但是集合中的每本书都有IDGREEP(外键)值0。我认为这个映射是不对的。请告诉我哪里错了。谢谢 以下是映射: <hibernate-mapping package = "model"> <class name = "User" table="virtual_bookcase.users"> <id name = "id" column

我尝试使用hibernate构建一些持久性应用程序(我的第一个)。该应用程序具有类型和书籍。体裁课有一套书。但是集合中的每本书都有IDGREEP(外键)值0。我认为这个映射是不对的。请告诉我哪里错了。谢谢

以下是映射:

<hibernate-mapping package = "model">

   <class name = "User" table="virtual_bookcase.users">
        <id name = "id" column = "id" type = "long">
            <generator class = "increment"/>
        </id>
        <property name = "username" column = "username" type = "string"/>
        <property name = "password" column = "password" type = "string"/>
   </class>

   <class name = "Genre" table = "virtual_bookcase.genres">
      <id name = "id" column = "idGenres" type = "long">
        <generator class = "increment"/>
      </id>   
      <property name = "name" column = "name" type = "string"/>
      <set name = "books" table = "virtual_bookcase.books" cascade = "all-delete-orphan">
        <key column = "idGenre" not-null = "true" />
        <one-to-many class = "Book"/>
      </set>
      <many-to-one name = "user" class = "User" column = "user_id" />
   </class>

   <class name = "Book" table = "virtual_bookcase.books">
        <id name = "id" column = "idBooks" type = "long">
         <generator class = "increment"/>
        </id>
        <property name = "title" column = "title" type = "string"/>
        <property name = "author" column = "author" type = "string"/>
        <property name = "publisher" column = "publisher" type = "string"/>
        <property name = "pages" column = "pages" type = "short"/>
        <property name = "borrowed" column = "borrowed" type = "byte"/>
        <property name = "borrowedTo" column = "borrowedTo" type = "string"/>
   </class>

</hibernate-mapping>
saveGenre(g)方法是(不初始化SessionFactory和session And transaction):

乍一看,你的地图还不错。我怀疑您可能没有正确地将您的
书籍
实体与相应的
类型
关联起来。你能发布代码来创建并保存
Book
s吗

更新:您的代码看起来可以工作,但不清楚为什么要合并您的类型(如果您在跨多个会话的对话中使用处于分离状态的实体,则可以,否则可能会使图片过于复杂)。缺少
Genre.addBook
的定义,但我认为你做得对:-)

我的新观察是,您尚未映射
Book.idGenre
。尝试按如下方式扩展映射:

<class name = "Book" table = "virtual_bookcase.books">
    ...
    <many-to-one name="idGenre" column="idGenre" class="Genre" not-null="true"/>
</class>
  <set name = "books" cascade = "all-delete-orphan">
    <key column = "idGenre"/>
    <one-to-many class = "Book"/>
  </set>

当然,还要更新getter/setter,并相应地进行映射。

@DaJackal,此代码仅定义
Book
类,并从数据库检索现有实例。如何创建
书籍
的新实例,并将它们与
流派
关联?我进行了更新,但是现在有两个例外:org.hibernate.PropertyAccessException:IllegalArgumentException在调用model.Book.idGenre和java.lang.IllegalArgumentException的setter时发生:参数类型不匹配谢谢你的帮助,Peter。现在看来这是可行的。但我觉得有些地方不对。在图书实体中,现在我有了一个类型字段。但在体裁实体中,我有一套书,所有体裁的书。现在我正在学习hibernate,我很好奇,没有其他的映射解决方案,其中book实体有IDGREEN字段,而不是GREEN字段?@DaJackal,用
GREEN
属性而不是
long
表示关联不是更自然吗?从OO的角度来看,id只是一个实现细节,它实际上将您与使用关系数据库的持久性实现联系在一起。Hibernate通过直接依赖对象属性而不是ID来避免这种依赖性。好的,感谢您的耐心和解释。祝您有个美好的一天!
Book b = new Book("AddedBook", "A", "A", (short) 555, (byte) 0, "", 1);
Genre g = ((Genre) cmbGenres.getSelectedItem());
g.addBook(b);
control.saveGenre(g);
   t = session.beginTransaction();
   Genre gr = (Genre) session.merge(g);
   t.commit();
<class name = "Book" table = "virtual_bookcase.books">
    ...
    <many-to-one name="idGenre" column="idGenre" class="Genre" not-null="true"/>
</class>
  <set name = "books" cascade = "all-delete-orphan">
    <key column = "idGenre"/>
    <one-to-many class = "Book"/>
  </set>
private Genre genre;