我想用Hibernate注释创建两个类 - Book和Bookshelf。本书提到了它的书架,书架上有一本书中包含的书籍。问题在于,当我尝试用新书架保存新书时,我在尝试访问书架的书时会遇到NullPointerException。
有趣的是,当我重新运行测试用例时,上次抛出异常的记录会被读取,但新添加的记录会崩溃应用程序。可能是什么原因?这是代码:
的src /主/ 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 /主/ 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 /主/ 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 /主/ 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 /主/资源/ 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>
答案 0 :(得分: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();
}
}