是的,它会工作。您可以使用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>
请注意,我还是要名单下跌实体类,但我没有使用Hibernate映射文件(hbm.xml)。我不认为Hibernate支持实体类的自动检测,就像JPA一样(即使它们被注释了,你仍然需要列出它们)。
然后,我有这样的代码作为测试,然后坚持实体使用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();
我有FF。在我的控制台输出:
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
来源
2015-10-16 11:47:09
Ish
我想这和它给这样的:在线程/ R/n'Exception“主” org.hibernate.HibernateException:当“hibernate.dialect”没有设置 连接不能为空\t在org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97) \t在org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67) \t at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)' – FaithReaper
它正在工作,因为我使用正确的方式在Hibernate 4.3.11中加载'hibernate.cfg.xml'。谢谢。其实我发现我的问题更多地涉及到“如何阅读hibernate.cfg.xml以使Hibernate Criteria正常工作”。我在这里留下了更多相关的[链接](http://stackoverflow.com/questions/7986750/create-session-factory-in-hibernate-4),以便有人找到我的答案。 – FaithReaper