但由于我对Hibernate不太熟悉,升级一直是一个挑战,因为我遇到了很多问题。虽然我能够解决大部分问题,但到目前为止,我无法对当前问题进行思考。现有的通用函数用于从表中获取所有实体。
根据条件创建查询,但原始SQL查询似乎错误,因此没有返回任何数据;更精确:表名和列名在其中不被替换。您可以在以下代码中找到的调试打印结果如下:
select generatedAlias0 from TestReferenceEntity as generatedAlias0
实体:
@Entity
@Table(name = "TEST_TYPE_REF")
@NamedQueries({@NamedQuery(name = "findTestEntityByName", query = "SELECT bt FROM TestReferenceEntity bt WHERE bt.name = :name") })
public class TestReferenceEntity extends AbstractEntity {
public static final String FIND_BY_NAME = "findTestEntityByName";
public static final String TYPENAME_PARAM_NAME = "name";
@NotNull
@Size(max = 50)
@Column(name = "NAME")
private String name;
@Size(max = 50)
@Column(name = "PREFIX")
private String prefix;
@Size(max = 100)
@Column(name = "IDENT_REGEXP")
private String regexp;
}
抽象DAO:
public abstract class AbstractDao<E extends AbstractEntity> implements AbstractService<E> {
public static final String PERSISTENCE_UNIT = "MeineJpaPU";
private static Logger LOG = Logger.getLogger(AbstractDao.class);
@PersistenceContext(unitName = PERSISTENCE_UNIT)
private EntityManager em;
@Inject
private UserService userService;
private E entityClass; //This is set elsewhere
public List<E> findAll() {
return findAll(1, 1000);
}
public List<E> findAll(final int page, final int rowsPerPage) {
final Class<E> clazz = getEntityClass(); //TestReferenceEntity.class
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<E> criteria = cb.createQuery(clazz);
final Root<E> r = criteria.from(clazz);
criteria.select(r);
final TypedQuery<E> query = getEntityManager().createQuery(criteria);
query.setFirstResult(page - 1);
query.setMaxResults((page - 1) * rowsPerPage);
System.out.println(">>>>>>>>>>>> " + query.unwrap(org.hibernate.Query.class).getQueryString());
return query.getResultList(); //Empty - but there is data in the table
}
}
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="MeineJpaPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:openejb/Resource/jdbc/PROJECTNAME</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
我最好的猜测是我遗漏了一些关键部分(注释?)在hibernate的更高版本中添加。毕竟我们使用的版本有点旧。
我已经研究过这种行为,但找不到任何与我的问题相匹配的东西。Hibernate的迁移指南也没有帮助。
丹尼尔