嗨,我刚开始冬眠。我正在尝试一个示例程序,当我第二次执行代码时,hibernate没有创建表并挂起。当我单击数据库表时,数据库挂起。
我使用的是PostgreSQL-9.3.3-1版本和hibernate-release-4.3.4.final。
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">postgres</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.pramati.dto.UserDetails"></mapping>
</session-factory>
2.我的POJO课。我在pojo类中使用注释
userdetails.java
@Entity
@Table (name="USER_DETAILS")
public class UserDetails {
@Id
private int userId;
private String userName;
@Temporal (TemporalType.DATE)
private Date joinDate;
private String address;
private String description;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
/**
* @param args
*/
public static void main(String[] args) {
UserDetails userDetails = new UserDetails();
userDetails.setUserId(1);
userDetails.setUserName("Avinash");
userDetails.setAddress("Rajeev Nagar");
userDetails.setJoinDate(new Date());
userDetails.setDescription("Software Engineer");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(userDetails);
session.getTransaction().commit();
session.close();
userDetails=null;
session = sessionFactory.openSession();
session.beginTransaction();
userDetails = (UserDetails)session.get(UserDetails.class, 1);
System.out.println("User Name Retrieved is " + userDetails.getUserName());
session.close();
}
Mar 26, 2014 3:49:06 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Mar 26, 2014 3:49:06 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
Mar 26, 2014 3:49:06 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 26, 2014 3:49:06 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 26, 2014 3:49:06 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Mar 26, 2014 3:49:06 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Mar 26, 2014 3:49:06 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Mar 26, 2014 3:49:06 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Mar 26, 2014 3:49:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Mar 26, 2014 3:49:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernatedb]
Mar 26, 2014 3:49:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=postgres, password=****}
Mar 26, 2014 3:49:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Mar 26, 2014 3:49:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 26, 2014 3:49:06 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect
Mar 26, 2014 3:49:06 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Mar 26, 2014 3:49:06 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mar 26, 2014 3:49:06 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Mar 26, 2014 3:49:07 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table USER_DETAILS cascade
Hibernate: create table USER_DETAILS (userId int4 not null, address varchar(255), description varchar(255), joinDate date, userName varchar(255), primary key (userId))
Mar 26, 2014 3:49:07 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into USER_DETAILS (address, description, joinDate, userName, userId) values (?, ?, ?, ?, ?)
Hibernate: select userdetail0_.userId as userId1_0_0_, userdetail0_.address as address2_0_0_, userdetail0_.description as descript3_0_0_, userdetail0_.joinDate as joinDate4_0_0_, userdetail0_.userName as userName5_0_0_ from USER_DETAILS userdetail0_ where userdetail0_.userId=?
User Name Retrieved is Avinash
Mar 26, 2014 3:50:52 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Mar 26, 2014 3:50:52 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
Mar 26, 2014 3:50:52 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 26, 2014 3:50:52 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 26, 2014 3:50:52 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Mar 26, 2014 3:50:52 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Mar 26, 2014 3:50:52 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Mar 26, 2014 3:50:52 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Mar 26, 2014 3:50:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Mar 26, 2014 3:50:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernatedb]
Mar 26, 2014 3:50:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=postgres, password=****}
Mar 26, 2014 3:50:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Mar 26, 2014 3:50:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 26, 2014 3:50:52 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect
Mar 26, 2014 3:50:52 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Mar 26, 2014 3:50:52 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mar 26, 2014 3:50:52 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Mar 26, 2014 3:50:52 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table USER_DETAILS cascade
尝试在main方法结束时关闭sessionFactory。
public static void main(String[] args) {
UserDetails userDetails = new UserDetails();
userDetails.setUserId(1);
userDetails.setUserName("Avinash");
userDetails.setAddress("Rajeev Nagar");
userDetails.setJoinDate(new Date());
userDetails.setDescription("Software Engineer");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(userDetails);
session.getTransaction().commit();
session.close();
userDetails=null;
session = sessionFactory.openSession();
session.beginTransaction();
userDetails = (UserDetails)session.get(UserDetails.class, 1);
System.out.println("User Name Retrieved is " + userDetails.getUserName());
session.close();
sessionFactory.close();
}
问题内容: 这个问题不太可能对将来的访客有所帮助;它仅与较小的地理区域,特定的时间段或极为狭窄的情况(通常不适用于Internet的全球受众)有关。要获得使该问题更广泛适用的帮助,请访问帮助中心。 7年前关闭。 我有带有Hibernate和Spring框架的Maven项目。我希望Hibernate自动创建表,但是只是删除所有现有表,并且不创建所需的表。在会话工厂初始化期间不会引发任何异常,但是当我
我的Hibernate查询SessionFactory sessionFactory=new Configuration(). confiure(). buildSessionFactory(); sess=sessionFactory.openSession()有问题;我在每个函数中都调用它,我如何避免它只创建一次sessionFactory
试图用hibernate创建表,但它已经创建并删除了。 hibernate.cfg.xml:
我希望hibernate创建三个表,但在我的情况下,只创建了一个表角色,我得到了如下错误 原因:org.hibernate.tool.schema.spi.schemaManagementException:无法对JDBC目标执行模式管理[创建表user_role(user_role_id numeric(19,0)标识not null,role_id numeric(19,0),user_id
我有两张桌子: 基本上,我想通过id字段定义一个从到的外键,这两个表中的字段是相同的。问题是,当我在IntelliJ Idea中检查数据库时,我在Keys部分没有看到任何外键(检查了两个表),只有它们的PK。这段代码有问题吗?我已经按照另一个类似问题中的建议设置了属性:
问题内容: 我有一个基本的Hibernate代码,我将属性“ hibernate.hbm2ddl.auto”设置为更新,因为它仍然无法在数据库中自动创建表。 这些是必需的文件: employee.hbm.xml liability.hbm.xml hibernate.cfg.xml 这是我尝试运行的Main.java: 这是我得到的错误: com.mysql.jdbc.exceptions.jdb