Tag: hibernate 4.x

Hibernate 4.3,在构建SessionFactory时为什么要提供两次属性?

当你想创建一个SessionFactory(例如unit testing)时,你需要使用Hibernate 4.3.5,你必须提供两次属性: 一旦进行配置 第二次,将设置应用于服务注册表生成器 该示例如下所示: Properties properties = new Properties(); properties.put(“hibernate.dialect”, “org.hibernate.dialect.HSQLDialect”); properties.put(“hibernate.connection.driver_class”, “org.hsqldb.jdbcDriver”); properties.put(“hibernate.connection.url”, “jdbc:hsqldb:mem:test”); properties.put(“hibernate.connection.username”, “sa”); properties.put(“hibernate.connection.password”, “”); properties.put(“hibernate.hbm2ddl.auto”, “update”); properties.put(“hibernate.show_sql”, “true”); SessionFactory sessionFactory = new Configuration() .addProperties(properties) .addAnnotatedClass(SecurityId.class) .buildSessionFactory( new StandardServiceRegistryBuilder() .applySettings(properties) .build() ); 如果我评论: //.addProperties(properties) 然后“hibernate.hbm2ddl.auto”属性没有彻底: Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: SECURITYID 如果我评论: //.applySettings(properties) 我得到: […]

Org.hibernate.HibernateException:没有活动事务,createQuery无效

在这个问题之后 ,我试图将Hibernate 4.3.8.Final添加到Spring 4.1.5.RELEASE; 但是 – 代码抛出一个exception,因为跟随并且有时抛出 – 它无法找到hibernate的实体类(com.myproject.model.MyTable),尽管该类存在且位于com.myproject.model包中。 堆栈跟踪 org.hibernate.HibernateException: createQuery is not valid without active transaction at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) at com.sun.proxy.$Proxy40.createQuery(Unknown Source) at com.myproject.repository.TestRepImpl.get(TestRepImpl.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy33.get(Unknown Source) at com.myproject.service.TestServiceImpl.get(TestServiceImpl.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at […]

Spring引导Hibernate错误java.lang.NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()Ljavax / persistence / ForeignKey;

我正在使用Spring Boot在具有许多Hibernate依赖项的现有项目上做原型。 我试图定义一个自定义LocalEntityManagerFactoryBean,它在这里我得到这个错误: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; 我已经尝试更新我的hibernate版本(由于项目向后兼容性导致许多其他编译错误,无法使用所有最新版本)但是使用以下hibernate依赖图可以使其编译但仍然得到此运行时依赖性错误。 当我搜索此错误时,所有结果都指向Hibernate 4和JPA 2.1的升级。 我们已经在使用Hibernate 4+,所以只是将版本更高一点但没有运气。 Gradle的以下依赖图显示我没有任何旧版本的“org.hibernate.javax.persistence:hibernate-jpa-2.0-api” | +— org.hibernate:hibernate-entitymanager:4.3.7.Final | | +— org.hibernate:hibernate-core:4.3.7.Final | | | +— org.hibernate.common:hibernate-commons-annotations:4.0.5.Final | | | +— org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final | | +— org.hibernate.common:hibernate-commons-annotations:4.0.5.Final (*) | | +— org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final | +— org.hibernate:hibernate-validator:5.1.3.Final | | | +— org.hibernate:ejb3-persistence:1.0.2.GA | | | | | +— org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final | | […]

如何使用Spring 4.0.6全局设置FlushMode for Hibernate 4.3.5.Final?

我正在尝试使用Hibernate 4.3.5.Final和Spring 4.0.6升级我们的应用程序。 我的应用程序中数据库写入操作的任何位置都会出现如下错误: Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition. at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135) at org.springframework.orm.hibernate4.HibernateTemplate$26.doInHibernate(HibernateTemplate.java:826) at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340) at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308) at org.springframework.orm.hibernate4.HibernateTemplate.deleteAll(HibernateTemplate.java:823) … 以下是sessionFactory和transactionManager的spring配置: com/mycompany/Person.hbm.xml org.hibernate.dialect.HSQLDialect 1: 为了全局设置flushMode以便应用程序以与以前相同的方式工作,我需要将flushMode设置为全局,因此我不想使用@Transactional(readOnly = false)方法。 2: 在下面的post中,有人建议将singleSession设置为false, Java / Hibernate – 在只读模式下不允许写入操作 Spring文档建议指定“singleSession”=“false”有副作用: http […]

Hibernate 4注释配置

我正在尝试仅使用带有注释的Hibernate 4和一个hibernate.cfg.xml文件。 我已经制作了自己的注释,并使用reflection将其添加到配置中。 我能够以这种方式使用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(); (不推荐的代码: buildSessionFactory(); )。 即使是hibernate 4文档也显示以这种方式构建配置。 如果我尝试使用新方法( buildSessionFactory(ServiceRegistry) ,我得不到相同的结果,并且看起来很多不必要的代码完全按照已弃用的方法执行。但是,我不想继续使用这种风格,因为我不喜欢使用已弃用的代码。 我的问题是:如何以上述方式从配置文件中正确配置Hibernate 4? 我似乎只是造成错误并面临不必要的困难。

为什么openSession不起作用,但getCurrentSession在Spring Hibernate中工作

我已经编写了一个示例Spring Hibernate应用程序o了解Spring hibernate集成的工作原理。 这是我的applicationContext.xml org.hibernate.dialect.Oracle10gDialect true 然后,我的服务类就是这样 package com.general; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; @Service(“employeeService”) public class EmployeeServiceImpl implements EmployeeService{ @Autowired private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Transactional public void saveEmployee(Employee emp) { Session session = sessionFactory.getCurrentSession();//.openSession(); session.save(emp); } } 而我的主要课程是 public […]

Tomcat:FAIL – 无法启动上下文路径/员工的应用程序

我是Spring MVC的新手。 试图使用安全登录来构建项目,但无法在tomcat中启动.war。 失败 – 无法启动上下文路径/员工的应用程序 我检查了libraris的重复,比如log4j和slf4j 请帮我理解原因,提前谢谢。 Spring MVC Application contextConfigLocation /WEB-INF/spring-security.xml /WEB-INF/applicationContext.xml spring org.springframework.web.servlet.DispatcherServlet 1 spring /employee/* org.springframework.web.context.ContextLoaderListener springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* 的pom.xml 4.0.0 ua.serge employee 1.0 war employee http://maven.apache.org employee-list UTF-8 3.1.2.RELEASE 4.1.7.Final Tomcat org.apache.maven.plugins maven-compiler-plugin 2.5.1 org.apache.maven.plugins maven-deploy-plugin 2.7 org.codehaus.mojo exec-maven-plugin 1.2.1 javassist javassist 3.12.1.GA javax.servlet jstl 1.2 mysql mysql-connector-java 5.1.9 […]

“使用名称创建bean时出错”创建事务管理器时

这是我的hibernate.cfg.xml com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/sometable root somepassword 1 org.hibernate.dialect.MySQLDialect true 这是我的applicationContext.xml 启动Tomcat后,我收到此错误 Dec 02, 2012 11:36:39 PM org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘transactionManager’ defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type […]

如何在JPA和Hibernate 4.3中使用SchemaExportTool

在Hibernate 4.3中删除了Ejb3Configuration类。 此类通常用于从持久性单元(persistence.xml文件)到SchemaExport工具创建hibernate配置文件。 作为将模式导出到.sql文件的简单替代方法,我使用以下代码: public static void export(String persistenceUnit, String exportFileName) { Map hash = new HashMap(); hash.put(“hibernate.hbm2ddl.auto”, “create-drop”); EntityManagerFactory factory = Persistence.createEntityManagerFactory( persistenceUnit, hash); org.hibernate.jpa.internal.EntityManagerFactoryImpl hibFactory = (org.hibernate.jpa.internal.EntityManagerFactoryImpl) factory; SessionFactoryImpl hibSessionFactory = hibFactory.getSessionFactory(); SchemaExport schema = ReflectUtils.getPrivateFieldValue( hibSessionFactory, “schemaExport”); schema.setOutputFile(exportFileName); schema.setFormat(false); schema.setDelimiter(“;”); schema.drop(true, false); schema.create(true, false); } 在这段代码中,我基本上使用的是由HibernateSessionFactoryImpl创建的schemaexport对象。 缺点是每次执行时都会重新创建数据库模式。 有没有其他简单的方法来使用SchemaExporTool与Hibernate 4.3和JPA? 似乎真正的问题是如何从持久性单元创建Hibernate配置对象?

hibernate 4.3.x – 加载所有实体注释类

在我工作的项目中,我不使用Spring,我只使用Hibernate。 我不想将hbm.xml文件用于实体映射/描述/等。 我只想使用注释。 如何告诉Hibernate从某些软件包加载所有Entity/Table注释类? 我在网上搜索但我没有运气。 另外,我找不到有关最新Hibernate版本的信息(主要是过时的文章/post等)。 编辑1: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#mapping hibernate doc页面说明了这一点: 对象/关系映射可以用三种方法定义: 1)使用Java 5注释(通过Java Persistence 2注释) 2)使用JPA 2 XML部署描述符(在第XXX章中描述) 3)使用称为hbm.xml的Hibernate遗留XML文件方法 就这样,我只想使用1)与SessionFactory / Session Hibernate API。 编辑2: 即使我的问题被标记为重复,但事实并非如此,因为我不想使用JPA(以及隐含的描述符),而只是使用上面列出的那些方法来处理1)。