0

我想用 Hibernate 注释创建两个类——Book 和 Bookshelf。Book 引用了它的 Bookshelf,Bookshelf 保存了它包含的书籍列表。问题是,当我尝试用新书架保存新书时,我在尝试访问书架的书时收到 NullPointerException。

有趣的是,当我重新运行测试用例时,上次引发异常的记录被读取,但新添加的记录使应用程序崩溃。这可能是什么原因?这是代码:

src/main/java/com/mkyong/App.java:

 package com.mkyong;

 import java.util.List;

 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.cfg.Configuration;

 import com.mkyong.pojo.Book;
 import com.mkyong.pojo.Bookshelf;
 import com.mkyong.util.HibernateUtil;


 public class App {
    public static void main(String[] args) {
            System.out.println("Hibernate one to one (Annotation)");
            new Configuration().configure().buildSessionFactory();

            Session session = HibernateUtil.getSessionFactory().openSession();
            testWriting(session);

            //https://www.youtube.com/watch?v=hAAlDoAtV7Y
            testReading(session);

            System.out.println("Done");
    }

    private static void testWriting(Session session) {
            session.beginTransaction();

            Bookshelf b2 = new Bookshelf();
            session.save(b2);

            Book b = new Book(b2);

            session.save(b);
            session.getTransaction().commit();
    }

    private static void testReading(Session session) {
            Query q = session.createQuery("from Bookshelf");
            List<Bookshelf> bookshelves = q.list();
            System.out.println("The list size is:" + bookshelves.size());
            for (Bookshelf b3 : bookshelves) {
                    System.out.println("b3.id=" + b3.getId());
                    List<Book> books = b3.getBooks();
                    for (Book book : books) {
                            System.out.println(book.getId());
                    }
            }
    }
 }

src/main/java/com/mkyong/pojo/Bookshelf.java:

 package com.mkyong.pojo;

 import java.util.List;

 import javax.persistence.CascadeType;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.OneToMany;

 import javax.persistence.Id;
 import javax.persistence.Table;



 @Entity
 @Table(name = "Bookshelf")
 public class Bookshelf implements java.io.Serializable {


     private Integer id;

     private List<Book> books;

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
     public Integer getId() {
            return id;
    }

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

    @OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy = "bookshelf")
    public List<Book> getBooks() {
            return books;
    }

    public void setBooks(List<Book> books) {
            this.books = books;
    }
 }

src/main/java/com/mkyong/pojo/Book.java:

 package com.mkyong.pojo;

 import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;

 import javax.persistence.Id;

 @Entity
 public class Book implements java.io.Serializable {


     private Integer id;

    private Bookshelf bookshelf;

    public Book()
    {

    }

    public Book(Bookshelf bookshelf)
    {
            this.bookshelf = bookshelf;  
    }

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
     public Integer getId() {
            return id;
    }

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

     @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "bookshelf_id", nullable = false)
     public Bookshelf getBookshelf() {
            return bookshelf;
    }

     public void setBookshelf(Bookshelf bookshelf) {
            this.bookshelf = bookshelf;
    }
 }

src/main/java/com/mkyong/util/HibernateUtil.java:

 package com.mkyong.util;

 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;

 public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
            try {
                    // Create the SessionFactory from hibernate.cfg.xml
                    return new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                    // Make sure you log the exception, as it might be swallowed
                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);
            }
    }

    public static SessionFactory getSessionFactory() {
            return sessionFactory;
    }

    public static void shutdown() {
            // Close caches and connection pools
            getSessionFactory().close();
    }

 }

src/main/resources/hibernate.cfg.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 <hibernate-configuration>

    <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/library</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password"></property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
         <property name="hibernate.hbm2ddl.auto">update</property>

            <property name="show_sql">true</property>
            <mapping class="com.mkyong.pojo.Book" />
            <mapping class="com.mkyong.pojo.Bookshelf" />

    </session-factory>

 </hibernate-configuration>

pom.xml:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.common</groupId>
    <artifactId>HibernateExample</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>HibernateExample</name>
    <url>http://maven.apache.org</url>

    <repositories>
            <repository>
                    <id>JBoss repository</id>
                    <url>http://repository.jboss.org/nexus/content/groups/public/</url>
            </repository>
    </repositories>

    <dependencies>

            <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.8.2</version>
                    <scope>test</scope>
            </dependency>

            <!-- MySQL database driver -->
            <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.15</version>
            </dependency>

    <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                    <version>4.1.4.Final</version>
            </dependency>

            <dependency>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>3.12.1.GA</version>
            </dependency>

            <!-- logback logging framework-->
            <dependency>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-core</artifactId>
                    <version>0.9.28</version>
            </dependency>

            <dependency>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                    <version>0.9.28</version>
            </dependency>

                            <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <version>1.3.161</version>
            </dependency>

    </dependencies>
 </project>
4

1 回答 1

1

你得到一个空指针异常,因为 Bookshelf 类中的书籍列表从未被初始化,也没有任何书籍被添加到其中。使对象图保持一致是您的责任。BookShelf 永远不应该有一个空的书籍列表。它可能有一个空列表。当您为给定的书架创建新书时,您还应该将书添加到书架中。

将您的代码更改为:

public class Bookshelf implements java.io.Serializable {
    private List<Book> books = new ArrayList<Book>(0);
    ...

    public void addBook(Book book) {
        books.add(book);
        book.setBookshelf(this);
    }
}

    private static void testWriting(Session session) {
        session.beginTransaction();

        Bookshelf b2 = new Bookshelf();
        session.save(b2);

        Book b = new Book();
        b2.addBook(b);

        session.save(b);
        session.getTransaction().commit();
    }
}
于 2013-05-31T20:59:46.180 回答