Spring 我可以在不映射Hibernate的情况下使用Hibernate标准吗?
我正在使用JPA注释来映射模型中的实体。然而,我发现Hibernate标准很容易使用,并且包含的代码更少,所以有没有一些方法可以在不映射HibernateXML的情况下使用标准?我在我的DAO实现类中尝试了这一点:Spring 我可以在不映射Hibernate的情况下使用Hibernate标准吗?,spring,hibernate,annotations,mapping,Spring,Hibernate,Annotations,Mapping,我正在使用JPA注释来映射模型中的实体。然而,我发现Hibernate标准很容易使用,并且包含的代码更少,所以有没有一些方法可以在不映射HibernateXML的情况下使用标准?我在我的DAO实现类中尝试了这一点: private SessionFactory sFactory; // of type org.hibernate.SessionFactory .... Session session = sFactory.getCurrentSession(); Criteria criter
private SessionFactory sFactory; // of type org.hibernate.SessionFactory
....
Session session = sFactory.getCurrentSession();
Criteria criteria = session.createCriteria(BTerminal.class);
但是,如果没有hibernate.cfg.xml
,它将给出nullpointerexception
。当然,因为它不是注射的。但是要填充这个cfg.xml
我必须添加映射xml文件,这不是我喜欢的方式。那么,我可以在使用Hibernate标准时使用JPA映射吗
我没有用弹簧。仍然怀疑哪一个更容易:编写包含所有atributes的10+映射XML,或者学习更多关于SpringDaoSupport的知识,或者任何其他方法
提前谢谢。是的,它会起作用的。您可以使用JPA注释实体,同时使用Hibernate条件来查询实体,而不是JPA条件 我已经测试过了 我的实体类如下所示:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class TestEntity {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Version
private Long version;
...
}
然后,我有了Hibernate配置文件:Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping class="com.test.model.TestEntity" />
</session-factory>
</hibernate-configuration>
我正在尝试此操作,它在线程“main”org.hibernate.HibernateException中给出了一个:/r/n
异常:当在org.hibernate.service.jdbc.dialent.internal.diallectFactoryImpl.determineDialect中未设置“hibernate.dialent”时,连接不能为null(diallectFactoryImpl.java:97)在org.hibernate.engine.jdbc.internal.jdbcservicesiml.configure(jdbcservicesiml.java:174)的org.hibernate.service.jdbc.dianal.internal.方言工厂impl.builddianal(方言工厂impl.java:67)
它正在工作,因为我在hibernate 4.3.11中使用了正确的加载方式hibernate.cfg.xml
。谢谢实际上,我发现我的问题更多地与“如何读取hibernate.cfg.xml以使hibernate标准工作”相关。我在这里留下一个更相关的答案,希望有人能找到我的答案。
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
TestEntity testEntity = new TestEntity();
testEntity.setName("test");
session.save(testEntity);
List<TestEntity> tests = (List<TestEntity>) session.createCriteria(TestEntity.class).list();
for (TestEntity test : tests) {
System.out.println(test.getName());
}
session.getTransaction().commit();
Hibernate: insert into TestEntity (name, version) values (?, ?)
Hibernate: select this_.id as id1_0_0_, this_.name as name2_0_0_, this_.version as version3_0_0_ from TestEntity this_
test