HSQL - 休眠。 org.hibernate.HibernateException:“....Author.class”实例的标识符已从 1 更改为 null

Posted

技术标签:

【中文标题】HSQL - 休眠。 org.hibernate.HibernateException:“....Author.class”实例的标识符已从 1 更改为 null【英文标题】:HSQL - HIBERNATE. org.hibernate.HibernateException: identifier of an instance of "....Author.class" was altered from 1 to null 【发布时间】:2014-02-05 12:18:41 【问题描述】:

无法将数据插入 HSQL DB(内存中)。请帮助我理解错误,来自控制台的文本:

    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    Hibernate: 
insert 
into
    author
    (ID, NAME, COUNTRY) 
values
    (default, ?, ?)
    Hibernate: 
insert 
into
    book
    (ID, NAME, GENRE, AUTHORID) 
values
    (default, ?, ?, ?)
    Hibernate: 
insert 
into
    book
    (ID, NAME, GENRE, AUTHORID) 
values
    (default, ?, ?, ?)
    org.hibernate.HibernateException: identifier of an instance of com.maven.vaadin.bookshelf.Author was altered from 1 to null
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:85)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.maven.vaadin.bookshelf.DBManager.save(DBManager.java:50)
at com.maven.vaadin.bookshelf.MyVaadinUI.init(MyVaadinUI.java:43)
at com.vaadin.ui.UI.doInit(UI.java:614)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
    Hibernate: 
select
    author0_.ID as ID0_,
    author0_.NAME as NAME0_,
    author0_.COUNTRY as COUNTRY0_ 
from
    author author0_
    List size: 0

连接到数据库的休眠配置和映射文件的链接

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings, Connect to HSQL, IN Memory  -->
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="connection.url">jdbc:hsqldb:mem:test</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property><property name="format_sql">true</property>
    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!--create the database schema on startup if required -->
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="com/maven/vaadin/bookshelf/Author.hbm.xml"/>
    <mapping resource="com/maven/vaadin/bookshelf/Book.hbm.xml"/>
</session-factory>

休眠映射文件。第一:

<hibernate-mapping package="com.maven.vaadin.bookshelf">
<class name="com.maven.vaadin.bookshelf.Author" table="author">
    <id name="id" type="java.lang.Long">
        <column name="ID" />
        <generator class="native" />
    </id>
    <property name="name" type="java.lang.String">
        <column name="NAME" length="64" not-null="true" unique="true" />
    </property>    
    <property name="country" type="java.lang.String">
        <column name="COUNTRY" length="32" not-null="true" />
    </property>    
    <set name="authorbook" table="book" cascade="all" inverse="true">
        <key column="AUTHORID" not-null="true" />
        <one-to-many class="com.maven.vaadin.bookshelf.Book" />
    </set>
</class>

第二:

<hibernate-mapping package="com.maven.vaadin.bookshelf">
<class name="com.maven.vaadin.bookshelf.Book" table="book">
    <id name="id" type="java.lang.Long">
        <column name="ID" />
        <generator class="native" />
    </id>
    <property name="name" type="java.lang.String">
        <column name="NAME" length="32" not-null="true" />
    </property>
    <property name="genre" type="java.lang.String">
        <column name="GENRE" length="32" not-null="true" />
    </property>    
    <many-to-one name="author" class="com.maven.vaadin.bookshelf.Author" not-null="true">
        <column name="AUTHORID" />
    </many-to-one>
</class>

Java:

public class Author implements Serializable
private Long id;
private String name;
private String country;
private Set<Book> authorbook = new HashSet<Book>();

public Author(String name, String country, Set<Book> authorBook)
    super();
    this.name = name;
    this.country = country;
    this.authorbook = authorbook;

public Author()

public void setId(Long Id)this.id = id;
public Long getId()return id;

public void setName(String name)this.name = name;
public String getName()return name;   

public void setCountry(String country)this.country = country;
public String getCountry()return country;

public void setAuthorbook(Set<Book> authorBook)this.authorbook = authorBook;
public Set<Book> getAuthorbook()return authorbook;

public void addBook(Book book)
    book.setAuthor(this);
    this.authorbook.add(book);


public class Book implements Serializable
private Long id;
private String name;
private String genre;
private Author author;

public Book(String name, String genre)
    super();
    this.name = name;
    this.genre = genre;

public Book()

public void setId(Long id)this.id = id;
public Long getId()return id;

public void setName(String name)this.name = name;
public String getName()return name;

public void setGenre(String genre)this.genre = genre;
public String getGenre()return genre;

public void setAuthor(Author author)this.author = author;
public Author getAuthor()return author;

public class DBManager 

public static void main(String[] args)
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    DBManager app = new DBManager();
    app.save("Author","Country");
    app.list();


public void save(String authorName, String authorCountry)
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Long id = null;
    Transaction transaction = null;

    try 
        transaction = session.beginTransaction();
        Author author = new Author();
        author.setName(authorName);
        author.setCountry(authorCountry);

        Book bk1 = new Book();
        Book bk2 = new Book();
        bk1.setName("Book1");;
        bk1.setGenre("Genre1");

        bk2.setName("Book2");;
        bk2.setGenre("Genre2");
        author.addBook(bk1);
        author.addBook(bk2);
        session.save(author);

        transaction.commit();
     catch (HibernateException e) 
        transaction.rollback();
        e.printStackTrace();
     finally 
        session.close();
    



public void list() 
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try 
        transaction = session.beginTransaction();
        @SuppressWarnings("unchecked")
        List<Author> list = session.createQuery("FROM Author").list();

        System.out.println("List size: " + (list).size());
        for (Iterator iterator = list.iterator(); iterator.hasNext();) 
            Author author = (Author) iterator.next();
        
        transaction.commit();
     catch (HibernateException e) 
        transaction.rollback();
        e.printStackTrace();
     finally 
        session.close();
    

【问题讨论】:

好吧,祝你好运。如果您对自己解决问题有任何疑问,请随时问他们:/ 【参考方案1】:

属性 id 的 setter 方法存在问题。你传递了 Id 而不是 idI 是大写) - 为此休眠抛出异常 您应该将 setter 方法参数名称从 Id 更改为 id,它会起作用。

public void setId(Long id) 
    this.id = id;

即使您从 this.id = id 更改为 this.id = Id 也可以。

public void setId(Long Id) 
    this.id = Id;

【讨论】:

在此站点上说“谢谢”的典型方式是单击此答案旁边的复选标记的轮廓。见***.com/help/someone-answers

以上是关于HSQL - 休眠。 org.hibernate.HibernateException:“....Author.class”实例的标识符已从 1 更改为 null的主要内容,如果未能解决你的问题,请参考以下文章

hibernate hsql得到错误节点没有数据类型:org.hibernate.hql.ast.tree.IdentNode

org.hibernate.HibernateException:没有使用 HSQL DB 配置 CurrentSessionContext

用于 HSQL 的宠物诊所 hibernate.dialect

休眠:org.hibernate.QueryException

休眠:object.org.hibernate.MappingException

休眠代码生成:org.hibernate.HibernateException:没有可用连接时必须设置“hibernate.dialect”