我是Hibernate的新手。我使用Postgres作为我的数据库。我创建了一个简单的book类,并尝试将数据存储到名为book
这是我的Book课程:
package com.techno.domain;
public class Book {
private int id;
private String name;
private String authur;
private String year_published;
private String genere;
private int no_of_piece;
/**
* Default Constructor
*/
public Book() {
super();
}
/**
* @param name
* @param authur
* @param year_published
* @param genere
* @param no_of_piece
*/
public Book(String name, String authur, String year_published, String genere, int no_of_piece) {
super();
this.name = name;
this.authur = authur;
this.year_published = year_published;
this.genere = genere;
this.no_of_piece = no_of_piece;
}
/**
* @param id
* @param name
* @param authur
* @param year_published
* @param genere
* @param no_of_piece
*/
public Book(int id, String name, String authur, String year_published, String genere, int no_of_piece) {
super();
this.id = id;
this.name = name;
this.authur = authur;
this.year_published = year_published;
this.genere = genere;
this.no_of_piece = no_of_piece;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the authur
*/
public String getAuthur() {
return authur;
}
/**
* @param authur
* the authur to set
*/
public void setAuthur(String authur) {
this.authur = authur;
}
/**
* @return the year_published
*/
public String getYear_published() {
return year_published;
}
/**
* @param year_published
* the year_published to set
*/
public void setYear_published(String year_published) {
this.year_published = year_published;
}
/**
* @return the genere
*/
public String getGenere() {
return genere;
}
/**
* @param genere
* the genere to set
*/
public void setGenere(String genere) {
this.genere = genere;
}
/**
* @return the no_of_piece
*/
public int getNo_of_piece() {
return no_of_piece;
}
/**
* @param no_of_piece
* the no_of_piece to set
*/
public void setNo_of_piece(int no_of_piece) {
this.no_of_piece = no_of_piece;
}
}
我的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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/library</property>
<property name="connection.username">test</property>
<property name="connection.password">password</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- <property name="hibernate.hbm2ddl.auto">validate</property> -->
<!-- Use XML-based mapping metadata -->
<mapping resource="com/techno/domain/Book.hbm.xml"/>
<!-- Use Annotation-based mapping metadata -->
<!-- <mapping class="com.techno.entity.Book" /> -->
</session-factory>
</hibernate-configuration>
我的Book.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.techno.domain.Book" table="book">
<id name="id" type="int" column="id">
<generator class="sequence">
<param name="sequence">book_id_seq</param>
</generator>
</id>
<property name="authur" type="string" column="authur" />
<property name="genere" type="string" column="genre" />
<property name="name" type="string" column="name" />
<property name="year_published" type="string" column="year_published" />
<property name="no_of_piece" type="int" column="no_of_piece" />
</class>
</hibernate-mapping>
我无法弄明白我做错了什么。我经常遇到这个错误。
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select nextval ('hibernate_sequence')
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:96)
at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:417)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at com.techno.libraryOwner.LibraryOwner.main(LibraryOwner.java:20)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
Position: 17
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
... 13 more
答案 0 :(得分:0)
试试这个:
<id name="id" type="int" column="id">
<generator class="sequence">book_id_seq
<param name="sequence">book_id_seq</param>
</generator>
</id>