Tag: hibernate

Hibernate中的错误简单示例初学者级别

为了学习hibernate,我写了两个练习的例子。 但是,它们都有如下错误: 无法创建sessionFactory object.java.lang.NoClassDefFoundError:javax / transaction / SystemException线程“main”中的exceptionjava.lang.ExceptionInInitializerError由以下引起:java.lang.NoClassDefFoundError:java.lang.Class.forName0中的javax / transaction / SystemException位于org.jboss.logging.logMer.logM的上的java.lang.Class.forName(未知来源)的(本机方法)org.jboss.logging.Logger.getMessageLogger(Logger.java:2214) at com.example.ManageEmployee.main上的org.hibernate.cfg.Configuration。(Configuration.java:184)(ManageEmployee.java:17) 基本上,我首先编写POJO,并使用eclipse生成hbm.xml。 之后,我编写了管理数据库的主要function。 我试过两次,但遇到了同样的问题。 有人可以给我建议解决这个问题吗? 在此之前,使用JDBC构建项目,但这太复杂了。 所以我需要学习hibernate。 谢谢。 补充(这个hibernate示例项目中的详细信息): 我的Eclipse项目名称: HibernateExa hibernate.cfg.xml : com.mysql.jdbc.Driver hibernateTest jdbc:mysql://localhost:3306/hibernatetest; hibernater hibernatetest org.hibernate.dialect.MySQLDialect create org.hibernate.cache.HashtableCacheProvider thread 我的POJO是Person.java 。 使用eclipse生成Person.hbm.xml 。 项目包含主要function:TestPerson.java public static void main(String [] args){ Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); createPerson(session); queryPerson(session); } public […]

NamingException:jta.UserTransaction

我有一个直接使用Java Transaction API资源的hibernate应用程序。 我正在尝试使用UserTransaction来完成一些基本的数据库事务 UserTransaction接口定义允许应用程序显式管理事务边界的方法 在hibernate.cfg.xml文件中,我有以下属性: true java:comp/env/myDataSource java:comp/UserTransaction org.hibernate.transaction.JTATransactionFactory 在应用程序的web.xml中,UserTransaction在 Object factory for MyBean instances. UserTransaction javax.transaction.UserTransaction 在代码中,我尝试使用InitialContext以其JNDI名称查找UserTransaction public UserTransaction getTransactionContext() throws Exception { if (this.ut == null) { ut = (UserTransaction)new InitialContext().lookup(“java:comp/UserTransaction”); } return ut; } 但是返回以下exception: 03:27:37,530 ERROR [someServlet:555] Error in someMethod javax.naming.NamingException: Cannot create resource instance at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117) at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321) at […]

在PLSQL Oracle中抛出特定的错误消息…在hibernate中捕获?

是否可以在PL / SQL oracle存储过程中抛出特定的错误消息,并在调用时在Hibernate中捕获它?

Spring3 / Hibernate3 / TestNG:一些测试给出了LazyInitializationException,有些则没有

前言:我在unit testing中遇到了LazyInitializationException,我很难理解它,你可以从我的问题中看到Spring , TestNG和Spring 3中的 数据库会话以及unit testingHibernate时的LazyInitializationException使用TestNG在Spring中使用的实体类 为了能够清楚地提出我的问题,我在GitHub上做了一个示例项目: http : //github.com/niklassaers/Sample-Spring3-App/在这个示例项目中,我重现了我面临的问题在我的Spring3 / Hibernate3 / TestNG项目中。 问题:我有两个unit testing,它们非常相似,使用相同的服务测试相同类的相同类。 一个运行,一个运行失败。 为什么失败的失败? (或者,为什么跑步者不会以同样的方式失败?) 这是失败的测试: @Test(timeOut=1000) public void Roles() { User mockUser = userService.read(1); Assert.assertNotNull(mockUser); Assert.assertTrue(mockUser.isValid()); Set roles = mockUser.getRoles(); int size = roles.size(); // This line gives a LazyInitializationException Assert.assertTrue(size > 0); } 完整代码( http://github.com/niklassaers/Sample-Spring3-App/blob/master/src/tld/mydomain/sample/entities/test/FailingUserUnitTest.java ) 这是运行测试: @Test […]

使用Hibernate将下拉列表值保存到Struts 2中的数据库

我想将下拉列表的选定值保存到数据库中。 首先加载index.jsp 。 从index.jsp ,当我们点击index.jsp注册URL时,我们可以去register.jsp 。 struts.xml : register.jsp login.jsp index.jsp : Register register.jsp : 动作类是: public class RegisterAction extends ActionSupport { String name, pwd, email, address, months; int phno; List allMonths = new ArrayList(); List users = new ArrayList(); UserDao udao = new UserDao(); public List getAllMonths() { return allMonths; } public void setAllMonths(List […]

HQL(Hibernate)如何检查元素列表是否是另一个列表的子集?

我很难创建一个HQL查询,检查表’X’的记录’a’列表是否包含在同一个表’X’的另一个列表’b’中。 这是我正在使用的一个例子: select r from Role r where ( select p from Permission p where p.name = ‘test’) in elements(r.permissions) 现在这个查询只是作为一个例子,为了澄清一点,每个角色都有1个或更多权限。 现在发生了什么,如果subselect(权限p)返回1行,所有工作正常。 但如果选择了更多,查询将失败,因为我正在尝试检查列表是否在列表中… 我相信我在这里遗漏了一些东西,所以请任何帮助都会被认可。 谢谢

查询JOIN FETCH性能问题

我有hibernate查询性能的问题,我无法弄清楚。 在下面的代码片段中,我需要选择具有至少一个映射和过滤映射的实体。 我正在使用FETCH JOIN来加载仅过滤的映射。 但在这种情况下,我的查询存在性能问题。 Hibernate说警告日志: org.hibernate.hql.ast.QueryTranslatorImpl – 使用collection fetch指定的firstResult / maxResults; 在记忆中应用! 当我省略FETCH JOIN并且只剩下JOIN查询时很快。 但结果我将所有映射加载到实体,这对我来说是不可接受的状态。 有没有办法提高查询性能? 映射表中有很多行。 HQL查询: select distinct e from Entity join fetch e.mappings as mapping where e.deleted = 0 and e.mappings is not empty and e = mapping.e and mapping.approval in (:approvals) 实体: @Entity @Table(name=”entity”) class Entity { … @OneToMany(mappedBy=”entity”, cascade=CascadeType.REMOVE, […]

java.lang.NullPointerException Hibernate与Ehc​​ache一起使用

我将Hibernate 4.1.2与Ehcache 2.4.3一起使用(在下载hibernate时与hibernate一起发布)。 我的hibernate.cfg.xml: com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc:sqlserver://localhost:1433;databaseName=Stock_indices xxx xxx true org.hibernate.dialect.SQLServerDialect update 5 20 300 50 30 true true org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory ehcache-entity.xml ehcache-entity.xml: mapping.xml 但是,我得到了这个例外: java.lang.NullPointerException at org.hibernate.cache.ehcache.internal.util.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:64) at org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory.start(SingletonEhCacheRegionFactory.java:91) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:281) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741) ………… 看来hibernate无法加载配置? 我该如何解决这个问题? (serlvet在没有使用Ehcache的情况下工作正常)。 我的servlet类: package stockdataservlet; import java.io.IOException; import java.math.BigDecimal; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; […]

Hibernate:映射未找到exception

从官方文档开始Hibernate。 我陷入了配置xml文件的最基本步骤。 虽然我已按照示例中的描述完成了所有操作,但无法找到The mapping file 。 这是例外: Exception in thread “main” org.hibernate.MappingNotFoundException: resource: testing/ground/beans/User.hbm.xml not found at org.hibernate.cfg.Configuration.addResource(Configuration.java:728) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2115) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2087) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2020) at org.hibernate.cfg.Configuration.configure(Configuration.java:1935) at org.hibernate.cfg.Configuration.configure(Configuration.java:1914) at testing.ground.test.HibernateTest.main(HibernateTest.java:14) 使用main()的代码: package testing.ground.test; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateTest { private SessionFactory sessionFactory; public static void main(String[] args) […]

Hibernate,liquibase和hsqldb的Id生成问题

我用liquibase创建了一个表: 生成以下sql ddl查询: CREATE TABLE employees (id BIGINT NOT NULL, name VARCHAR(50) NOT NULL, CONSTRAINT PK_EMPLOYEES PRIMARY KEY (id)); 相应的实体: @Entity @Table(name=”employees”) public class EmployeeAccessProperty … @Id @GeneratedValue public long getId() { return id; } … 现在,当我尝试通过JPA实现保存它时,生成sql查询以插入数据: Hibernate: insert into employees (id, name) values (default, ?) 2013-05-20T14:29:22.525+0700 WARN SQL Error: -5544, SQLState: 42544 2013-05-20T14:29:22.526+0700 ERROR […]