gpt4 book ai didi

java - Hibernate将null分配给select的最后一条记录?

转载 作者:行者123 更新时间:2023-12-01 14:30:49 28 4
gpt4 key购买 nike

我想创建两个带有 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>

最佳答案

您会得到一个空指针异常,因为 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();
}
}

关于java - Hibernate将null分配给select的最后一条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16865636/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com