我正在尝试使用pgadmin3连接到ubuntu中的Postgresql9.1。我的Pgadmin3
GUI工具没有提供任何通过右键单击数据库来创建表的选项,但是在我看到的一些视频中可用。因此,我使用终端创建数据库,该数据库显示在pgadmin3中。
我的档案结构
我的Userdetails文件
package org.nitish.hiber;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int userId;
private String userName;
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;
}
}
我的HibernateCaller文件
package org.nitish.caller;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.nitish.hiber.UserDetails;
public class HibernateTest {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserId(1);
user.setUserName("First User");
try {
SessionFactory sessionFactory = new Configuration().configure("/HibernateTest/src/hibernate.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
} catch(HibernateException e) {
e.printStackTrace();
}
}
}
Hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver </property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hiber</property>
<property name="connection.username">nitish</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="org.nitish.hiber.UserDetails"/>
</session-factory>
</hibernate-configuration>
我收到以下错误
Mar 1, 2016 9:02:48 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 1, 2016 9:02:48 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/HibernateTest/src/hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at org.nitish.caller.HibernateTest.main(HibernateTest.java:17)
进行少量更改后(无法在finally块中使用session.close(),我认为这不应导致此错误)
Mar 1, 2016 10:13:34 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Mar 1, 2016 10:13:34 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 1, 2016 10:13:34 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 1, 2016 10:13:34 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Mar 1, 2016 10:13:35 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 1, 2016 10:13:35 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at org.nitish.caller.HibernateTest.main(HibernateTest.java:17)
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:229)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:161)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:117)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:73)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
... 14 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgresql.Driver
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226)
... 25 more
如果您hibernate.cfg.xml
在源文件夹的根目录中,只需执行
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
如果它在软件包中(例如),则org.nitish.caller
通过这种方式指定路径
SessionFactory sessionFactory = new Configuration()
.configure("/org/nitish/caller/hibernate.cfg.xml").buildSessionFactory();
您需要关闭session
(在finally
块中)。不要忘记添加rollback
代码。
请在中添加@Table
注释UserDetails
。
更新资料
Hibernate找不到org.postgresql.Driver
类的错误原因。它位于postgresql
jar中。您的图像上有那个jar,但是可能您没有将其添加到类路径中。请参阅如何在Eclipse(Java)中将JAR添加到项目构建路径。
要session
在finally
块中关闭a ,您需要session
在try
块外部具有变量。
Session session = sessionFactory.openSession();
try{
} finally {
session.close();
}
问题内容: 我在IntelliJ IDE中的 hibernate.cfg.xml 有问题。 这是我的休眠配置文件: 这是我的应用程序类: 我通过运行以下代码得到错误: 因此在HibernateManager 中构建似乎有些问题。我得到了错误 找不到cfg.xml资源 和以下堆栈跟踪: 我已经将hibernate.cfg.xml文件放在/ src / main / java / resources下
问题内容: 我在 项目根文件夹中* 有 hibernate.cfg.xml 文件。 * 如果我运行的应用程序包含: 它在方法中崩溃并显示以下消息: 无法找到cfg.xml资源[hibernate.cfg.xml] 输出 : 问题答案: 该 hibernate.cfg.xml的 文件应该被移动到 SRC 文件夹中。
问题内容: 我无法在Eclipse中运行简单的JMH基准测试。Maven依赖项: Java代码: 运行结果: 线程“主”中的异常java.lang.RuntimeException:错误:无法找到资源:org.openjdk上的org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:9
问题内容: 我该如何告诉Maven对无法在其中找到依赖项的存储库隐藏“无法找到资源” INFO消息,但显示那些确实找到依赖项的存储库呢?也就是说,显示匹配,但忽略未命中,因为这些淹没并模糊了匹配。 例如,代替此输出: 我希望Maven仅输出以下内容: 问题答案: 我不知道从Maven内部做什么,但是您总是可以将输出通过管道传递给grep或sed之类的进程,以删除每个模式的行数。
我有一个可运行的jar文件,无法访问位于默认src目录之外的资源。根据我的理解,这两门课的区别是什么。getResource()和ClassLoader。getResource(),我应该能够访问root/res/img/img1。png(请参阅下面的文件夹设置),方法是使用以下getResourceFile函数: 文件夹设置: 当我尝试执行jar可执行文件本身时,问题就出现了。然而,奇怪的是,我
我正在做一个项目,编译并运行我的IDE工作正常。所有资源都找到了,它正常工作。然而,当我构建我的. jar时,它能够找到一些资源,但不是全部。我的项目结构看起来是这样的: 当我运行jar文件时,它能够找到图像并使用它们,但是当我尝试访问html文件时,它不会打开。下面是jar的项目结构; 我以前从未处理过jar文件,所以这似乎是我犯的一个非常明显的错误。感谢您的帮助,谢谢! 编辑:访问找不到的文件