我正在尝试将guice,jpa和hibernate结合起来。 hibernate的初始化看起来不错,直到我尝试通过DAO访问映射类。我找到了所有的帮助请求,但我的问题是(现在)不是“自定义类”(至少我不这么认为)。
一起没什么特别的。我得到了我的persistence.xml,它定义了我的类和其中一个类的基本(persist,findById,remove)DAO。
public abstract class AbstractDao<E, K> implements Dao<E, K> {
@Inject
private EntityManager manager;
private final Class<E> entityClass;
@SuppressWarnings("unchecked")
public AbstractDao() {
final ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
}
@Override
public void persist(final E entity) {
this.manager.persist(entity);
}
@Override
public void remove(final E entity) {
this.manager.remove(entity);
}
@Override
public E findById(final K id) {
return this.manager.find(this.entityClass, id);
}
}
如果我尝试使用该DAO访问数据库
final LayerDao dao = this.layerDaoProvider.get();
final Layer layer = dao.findById(386L); //Tried with BigDecimal, Long
我收到以下错误(我在 java.lang.Long 中得到完全相同的错误):
Caused by: org.hibernate.UnknownEntityTypeException: Unable to locate persister: java.math.BigDecimal
at org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:637) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2909) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.internal.SessionImpl.access$1800(SessionImpl.java:203) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2660) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2646) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1181) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3357) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3332) ~[hibernate-core-5.2.2.Final.jar:5.2.2.Final]
[..]
我没想到会遇到一些java-core类的问题。
这是我配置的所有gradle依赖项:
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.6.2'
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
// https://mvnrepository.com/artifact/com.google.inject/guice
compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
// https://mvnrepository.com/artifact/com.google.inject.extensions/guice-persist
compile group: 'com.google.inject.extensions', name: 'guice-persist', version: '4.1.0'
// https://mvnrepository.com/artifact/com.cathive.fx/fx-guice
compile group: 'com.cathive.fx', name: 'fx-guice', version: '8.0.0'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2+'
我错过了什么?您还需要更多信息来帮助我吗?
编辑#1:我尝试访问的映射类。它是使用Eclipse(最新的JEE版)自动创建的:
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
/**
* The persistent class for the T_M_LAYER_NEU database table.
*
*/
@Entity
@Table(name="T_M_LAYER_NEU")
@NamedQuery(name="Layer.findAll", query="SELECT l FROM Layer l")
public class Layer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="T_M_LAYER_NEU_ID_GENERATOR", sequenceName="FIG_SYS_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="T_M_LAYER_NEU_ID_GENERATOR")
@Column(unique=true, nullable=false, precision=38)
private long id;
@Column(nullable=false, precision=1)
private BigDecimal component;
@Column(name="DISPLAY_ORDER", nullable=false, precision=3)
private BigDecimal displayOrder;
@Column(nullable=false, precision=1)
private BigDecimal exklusiv;
@Column(name="GEOMETRIETYP_ID_FK", precision=2)
private BigDecimal geometrietypIdFk;
@Temporal(TemporalType.DATE)
@Column(name="GUELTIG_BIS", nullable=false)
private Date gueltigBis;
@Temporal(TemporalType.DATE)
@Column(name="GUELTIG_VON", nullable=false)
private Date gueltigVon;
@Column(nullable=false, precision=1)
private BigDecimal isactive;
@Column(nullable=false, precision=1)
private BigDecimal iskulisse;
@Column(nullable=false, precision=1)
private BigDecimal isoverview;
@Column(name="LAYERTYP_ID_FK", nullable=false, precision=2)
private BigDecimal layertypIdFk;
@Column(nullable=false, length=80)
private String name;
@Column(name="R_O", nullable=false, precision=1)
private BigDecimal rO;
@Column(name="SOURCE_ID", precision=38)
private BigDecimal sourceId;
@Column(name="\"SYSTEM\"", nullable=false, precision=1)
private BigDecimal system;
@Column(name="USE_COMPLETECACHE", nullable=false, precision=1)
private BigDecimal useCompletecache;
@Column(name="XML_DEF", length=100)
private String xmlDef;
//bi-directional many-to-one association to Label
@ManyToOne
@JoinColumn(name="LABEL_ID_FK")
private Label label;
//bi-directional many-to-one association to Symbol
@ManyToOne
@JoinColumn(name="SYM_ID_FK")
private Symbol symbol;
//bi-directional many-to-one association to Tabellen
@ManyToOne
@JoinColumn(name="LAYERDEF_ID_FK")
private Tabellen table;
public Layer() {
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public BigDecimal getComponent() {
return this.component;
}
public void setComponent(BigDecimal component) {
this.component = component;
}
public BigDecimal getDisplayOrder() {
return this.displayOrder;
}
public void setDisplayOrder(BigDecimal displayOrder) {
this.displayOrder = displayOrder;
}
public BigDecimal getExklusiv() {
return this.exklusiv;
}
public void setExklusiv(BigDecimal exklusiv) {
this.exklusiv = exklusiv;
}
public BigDecimal getGeometrietypIdFk() {
return this.geometrietypIdFk;
}
public void setGeometrietypIdFk(BigDecimal geometrietypIdFk) {
this.geometrietypIdFk = geometrietypIdFk;
}
public Date getGueltigBis() {
return this.gueltigBis;
}
public void setGueltigBis(Date gueltigBis) {
this.gueltigBis = gueltigBis;
}
public Date getGueltigVon() {
return this.gueltigVon;
}
public void setGueltigVon(Date gueltigVon) {
this.gueltigVon = gueltigVon;
}
public BigDecimal getIsactive() {
return this.isactive;
}
public void setIsactive(BigDecimal isactive) {
this.isactive = isactive;
}
public BigDecimal getIskulisse() {
return this.iskulisse;
}
public void setIskulisse(BigDecimal iskulisse) {
this.iskulisse = iskulisse;
}
public BigDecimal getIsoverview() {
return this.isoverview;
}
public void setIsoverview(BigDecimal isoverview) {
this.isoverview = isoverview;
}
public BigDecimal getLayertypIdFk() {
return this.layertypIdFk;
}
public void setLayertypIdFk(BigDecimal layertypIdFk) {
this.layertypIdFk = layertypIdFk;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getRO() {
return this.rO;
}
public void setRO(BigDecimal rO) {
this.rO = rO;
}
public BigDecimal getSourceId() {
return this.sourceId;
}
public void setSourceId(BigDecimal sourceId) {
this.sourceId = sourceId;
}
public BigDecimal getSystem() {
return this.system;
}
public void setSystem(BigDecimal system) {
this.system = system;
}
public BigDecimal getUseCompletecache() {
return this.useCompletecache;
}
public void setUseCompletecache(BigDecimal useCompletecache) {
this.useCompletecache = useCompletecache;
}
public String getXmlDef() {
return this.xmlDef;
}
public void setXmlDef(String xmlDef) {
this.xmlDef = xmlDef;
}
public Label getLabel() {
return this.label;
}
public void setLabel(Label label) {
this.label = label;
}
public Symbol getSymbol() {
return this.symbol;
}
public void setSymbol(Symbol symbol) {
this.symbol = symbol;
}
public Tabellen getTable() {
return this.table;
}
public void setTable(Tabellen table) {
this.table = table;
}
}
编辑#2:使用过的DAO-Class。除了泛型的类型定义外,其他内容都没有:
public class LayerDao extends AbstractDao<Layer, Long> {
}