我有实体
@Entity
@Table(
name = SomeEntity.TABLE_NAME,
uniqueConstraints = {@UniqueConstraint(columnNames = {"first", "second"})}
)
public class SomeEntity extends SomeSuperEntity {
public static final String TABLE_NAME = "SomeEntity";
public SomeEntity() {
}
public SomeEntity(String first, String second, Integer third) {
super(first, second, third);
}
}
此实体的超类
@MappedSuperclass
public abstract class SomeSuperEntity extends SomeSuperAbstractEntity {
@Column(nullable = false)
private String first;
@Column(nullable = false)
private String second;
protected SomeSuperEntity () {
}
protected SomeSuperEntity (String first, String second, Integer third) {
super(third);
this.first= first;
this.second= second;
}
}
二等舱
@MappedSuperclass
public abstract class SomeSuperAbstractEntity {
@Column(nullable = false)
private Integer third;
protected SomeSuperAbstractEntity () {
}
protected SomeSuperAbstractEntity (Integer third) {
this.third= third;
}
}
我有两个方法,第一个是查找实体
public <T extends SomeSuperEntity > Optional<T> findByFirstAndSecond(String first, String second, Class<T> tClass) {
String hql = String.format("FROM %s E WHERE E.first = :first AND E.second = :second", getTable(tClass));
return jpaApi.em().createQuery(hql, tClass)
.setParameter("first", first)
.setParameter("second", second)
.getResultList().stream().findAny();
}
第二个用于将实体名称获取到hql
private String getTable(Class<? extends SomeSuperEntity > tClass) {
if (tClass.equals(SomeEntity .class)) {
return SomeEntity.TABLE_NAME;
}
return null;
}
当我尝试调用findbyfirstandsecond(“1”,“2”,someentity.class)时,出现了错误
[错误]c.i.somemethod-错误:org.hibernate.hql.internal.ast.querysyntaxexception:someentity未Map[来自someentity e,其中e.first=:first和e.second=:second]
1条答案
按热度按时间qyyhg6bp1#
您是否在应用程序上下文中定义了要扫描的包?
或者,在你身上定义它
hibernate.cfg.xml
```