我的项目是这样的:
/src/main/java
-thegamers
-app.java
-hibernateutil.java
Run Code Online (Sandbox Code Playgroud)
谁能告诉我在哪里放hibernate.cfg.xml?
因为我收到了这个错误:
Initial SessionFactory creation failed.org.hibernate.HibernateException: hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
at thegamers.HibernateUtil.<clinit>(HibernateUtil.java:8)
at thegamers.App.main(App.java:15)
Caused by: org.hibernate.HibernateException: hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2149)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2130)
at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 2 more
Run Code Online (Sandbox Code Playgroud) 这是MySQL的配置文件:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">zgy01</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="model.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
要为SQL Server 2005指定什么?我是这样做的:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">lal</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="model.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
更确切地说,我的问题是如何指定我必须连接的数据库?
在MySQL中,我曾经这样做过:
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
Run Code Online (Sandbox Code Playgroud) 我只是想学习Hibernate(最终版本4)但是在尝试创建会话工厂时我遇到了问题.以下是与此问题相关的一些代码:
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="connection.url">jdbc:mysql://localhost/fitterblog</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<!--<mapping class="com.fitterblog.objects.Article"/>
<mapping class="com.fitterblog.objects.Nav"/>
<mapping class="com.fitterblog.objects.Tag"/>
<mapping class="com.fitterblog.objects.User"/>-->
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
HibernateUtil.java:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
ServiceRegistryBuilder srb = new ServiceRegistryBuilder();
//NOTE: THIS IS WHERE MY PROGRAM DIES!! …
Run Code Online (Sandbox Code Playgroud) 当我尝试进行 hibernate 配置时,我将 hibernate dtd 放入 hibernate.cfg.xml 文件中,并且显示错误,禁止从外部资源下载。你能帮我解决这个错误吗?
我想用 hibernate 创建一个 Maven 项目,所以我尝试做我的 hibernate 配置来进行映射。
我正在尝试使用仅带注释的Hibernate 4和一个hibernate.cfg.xml
文件.我已经制作了自己的注释,并使用反射将其添加到配置中.我能够以这种方式使用Hibernate 4,但我的配置是使用不推荐的方法构建的.
final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(Item.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
return configuration.configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
(不推荐的代码:) buildSessionFactory();
.
即使是hibernate 4文档也显示以这种方式构建配置.
如果我尝试使用新方法(buildSessionFactory(ServiceRegistry)
我没有得到相同的结果,似乎有很多不必要的代码完全按照不推荐使用的方法做的那样.但是,我不想继续使用这种风格,因为我不喜欢使用已弃用的代码.
我的问题是:如何以上述方式从配置文件中正确配置Hibernate 4?我似乎只是造成错误并面临不必要的困难.
java hibernate hibernate.cfg.xml hibernate-annotations hibernate-4.x
我正在编写一个Web应用程序,我必须添加hibernate.我配置了maven(pom.xml)等,但现在我收到以下错误:
exception
javax.servlet.ServletException: org.hibernate.HibernateException: /hibernate.cfg.xml not found
Run Code Online (Sandbox Code Playgroud)
我正在使用NetBeans.我尝试将此文件移动到WEB-INF,根项目文件夹,src目录(默认包)但它仍然无法正常工作.我能怎么做?我不想像以下那样以编程方式设置此文件的路径:
Configuration cfg = new Configuration();
cfg.addResource("/some/path/to/this/file/Hibernate.cfg.xml");
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Envers将更新/插入审核到我的表中。我已经在数据库中创建了扩展名为_AUDIT的审计表。
但是,当我实际运行该应用程序时,在审计表中看不到任何条目。我什至没有抛出任何错误或异常。条目被插入到主表中,但AUDIT表未更新。
这是我的ENVERS配置:
hibernate.cfg.xml:
<!-- Hibernate ENVERS Configuration -->
<property name="org.hibernate.envers.audit_table_suffix">_AUDIT</property>
<property name="org.hibernate.envers.revision_field_name">REVISION_ID</property>
<property name="org.hibernate.envers.revision_type_field_name">REVTYPE</property>
<property name="org.hibernate.envers.do_not_audit_optimistic_locking_field">true</property>
<property name="org.hibernate.envers.default_schema">ROCC</property>
<!-- Hibernate ENVERS Listener Configuration -->
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-insert"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-update"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-delete"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-update"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-remove"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-collection-recreate"/>
Run Code Online (Sandbox Code Playgroud)
我的表如下:
/**
* Transaction generated by hbm2java
*/
@Audited
@Entity
@Table(name = "TRANSACTION", schema = "ROCC")
public class TransactionTable implements java.io.Serializable{...}
Run Code Online (Sandbox Code Playgroud)
审核表在同一架构中为TRANSACTION_AUDIT。
谁能告诉我为什么审计无效?
我收到此错误,我不知道我的hbm.xml文件有什么问题.我使用Eclipse Mars和Hibernate 5.0.2.
错误:
Nov 15, 2015 11:49:19 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
Nov 15, 2015 11:49:19 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 15, 2015 11:49:19 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 15, 2015 11:49:19 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
Nov 15, 2015 11:49:20 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Nov 15, 2015 11:49:20 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator …
Run Code Online (Sandbox Code Playgroud) 我想在我的hibernate项目中使用二级缓存,但我只知道一些关于hibernate二级缓存的问题,任何人都可以解释一下我在代码中使用这个问题以及我需要什么配置和.jar文件?我将这些设置设置为我的hibernate.cfg.xml文件
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
Run Code Online (Sandbox Code Playgroud)
并添加这些jar文件ehcache-1.6.1.jar, ehcache-1.6.1-javadoc.jar, ehcache-1.6.1-sources.jar
我想知道shoud我改变任何其他配置?
如何理解我的项目使用二级缓存?
如果只是把这个设置,hibernate自动使用这个,或者我必须在我的.java类中使用ant其他代码(比如任何注释或其他东西)
caching hibernate ehcache hibernate.cfg.xml second-level-cache
我在项目根文件夹中有hibernate.cfg.xml文件.
如果我运行的应用程序包含:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
它在configure()
方法中崩溃,并带有以下消息:
找不到cfg.xml资源[hibernate.cfg.xml]
输出:
Jun 17, 2016 12:04:59 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.max_fetch_depth=5, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.connection.pool_size=5}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [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 …
Run Code Online (Sandbox Code Playgroud)