英文:
org.hibernate.AnnotationException:No identifier specified for entity with XML Mapping
问题
I can see that you're encountering issues with your JPA configuration and annotations. Below is the translated version of your provided content:
我正在处理一个JPA程序,并且需要通过xml映射为一个类编写注解。我将我的模型类编写如下:
// Java代码
ppackage model;
import javax.persistence.*;
import java.util.Date;
public class Reservierung {
public Reservierung(Date datum, int praemienMeilenBonus, int preis, StatusInfo status, Zug zug, Strecke strecke, Benutzer benutzer, Zahlung zahlung) {
// 构造函数内容...
}
// 属性和方法...
}
之后,我创建了名为orm的xml映射文件:
// XML代码
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
version="2.0">
<!-- 映射定义... -->
</entity-mappings>
最后,我将orm.xml和所有其他文件添加到了我的persistence.xml中,现在看起来像这样:
// XML代码
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="westbahn">
<!-- 配置... -->
</persistence-unit>
</persistence>
但是,你遇到了一些问题,包括配置和注解。根据你提供的信息,我无法直接解决你的问题。你可能需要仔细检查以下几点:
- 确保你的类路径和包名设置正确。
- 检查类和属性的拼写是否正确。
- 确保所有需要的依赖库都已添加到项目中。
- 检查注解和映射是否正确配置。
- 确保没有重复映射或重复添加类到持久化单元。
如果你已经按照上述步骤操作并仍然遇到问题,可能需要详细检查错误日志以获得更多信息,以便确定问题所在。另外,考虑尝试不同的配置选项以解决问题。
英文:
I'm working on a JPA program and had to write annotations for one class via xml-mapping. I wrote my model class like that:
ppackage model;
import javax.persistence.*;
import java.util.Date;
public class Reservierung {
public Reservierung(Date datum, int praemienMeilenBonus, int preis, StatusInfo status, Zug zug, Strecke strecke, Benutzer benutzer, Zahlung zahlung) {
this.datum = datum;
this.praemienMeilenBonus = praemienMeilenBonus;
this.preis = preis;
this.status = status;
this.zug = zug;
this.strecke = strecke;
this.benutzer = benutzer;
this.zahlung = zahlung;
}
private Long ID;
private Date datum;
private int praemienMeilenBonus = 15;
private int preis = 150;
private StatusInfo status;
private Zug zug;
private Strecke strecke;
private Benutzer benutzer;
private Zahlung zahlung;
public Date getDatum() {
return datum;
}
public void setDatum(Date datum) {
this.datum = datum;
}
public int getPraemienMeilenBonus() {
return praemienMeilenBonus;
}
public void setPraemienMeilenBonus(int praemienMeilenBonus) {
this.praemienMeilenBonus = praemienMeilenBonus;
}
public int getPreis() {
return preis;
}
public void setPreis(int preis) {
this.preis = preis;
}
public StatusInfo getStatus() {
return status;
}
public void setStatus(StatusInfo status) {
this.status = status;
}
public Zug getZug() {
return zug;
}
public void setZug(Zug zug) {
this.zug = zug;
}
public Strecke getStrecke() {
return strecke;
}
public void setStrecke(Strecke strecke) {
this.strecke = strecke;
}
public Benutzer getBenutzer() {
return benutzer;
}
public void setBenutzer(Benutzer benutzer) {
this.benutzer = benutzer;
}
public Zahlung getZahlung() {
return zahlung;
}
public void setZahlung(Zahlung zahlung) {
this.zahlung = zahlung;
}
public Long getID() {
return ID;
}
public void setID(Long ID) {
this.ID = ID;
}
}
and after that I created the xml mapping file that I named orm:
?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
version="2.0">
<named-query name="Reservierung.findAll">
<query>SELECT b FROM model.Reservierung b</query>
</named-query>
<entity name="Reservierung" class="model.Reservierung" >
<table name="Reservierung" />
<attributes>
<id name="ID">
<generated-value strategy="AUTO" />
</id>
<basic name="datum" />
<basic name="praemienMeilenBonus" />
<basic name="preis" />
<basic name="StatusInfo" />
<one-to-one name="strecke" />
<one-to-one name="zug"/>
<one-to-one name="benutzer"/>
<transient name="zahlung"/>
</attributes>
</entity>
</entity-mappings>
finnaly I added the orm.xml and all the other files in my persistence.xml, which looks now like this
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="westbahn">
<description> Hibernate JPA Configuration Example</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>model.Bahnhof</class>
<class>model.Benutzer</class>
<class>model.Reservierung</class>
<class>model.Einzelticket</class>
<class>model.Preisstaffelung</class>
<class>model.Sonderangebot</class>
<class>model.Strecke</class>
<class>model.Ticket</class>
<class>model.Zeitkarte</class>
<class>model.Zug</class>
<class>model.Reservierung</class>
<properties>
<!--
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/westbahn?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="westbahnUser" />
<property name="javax.persistence.jdbc.password" value="westbahnPassword" />
-->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:file:./db/testing" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<!--<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>-->
</properties>
</persistence-unit>
</persistence>
Till now I tried to see if the names where spelled wrong, if my class had some attributes that I didn't account for and also tried to change the name and finally added access="field" to the entity tag, but if I do so, I get another exception on top of the first one: org.hibernate.AnnotationException: Unable to load class defined in XML: model.Reservierung
Where did I do wrong?
edit:
Before the new orm.xml I had different mapping file named Reservierung.hbm.xml
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Reservierung" table="Reservierung">
<meta attribute = "class-description">
This class contains the reservations
</meta>
<id name = "ID" type = "long" column = "ID">
<generator class="native"/>
</id>
<property name = "datum" column = "datum" type = "date"> </property>
<property name = "praemienMeilenBonus" column = "praemienMeilenBonus"> </property>
<property name = "preis" column = "preis"> </property>
<property name = "status" column = "status">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">model.StatusInfo</param>
</type>
</property>
<one-to-one name="strecke" />
<one-to-one name="zug"/>
<one-to-one name="benutzer"/>
</class>
</hibernate-mapping>
I wasn't happy with it, so I trashed it and started from scratch with the orm.xml file
edit: what I did:
I made a constructor + getter and setter in all classes and now the exception changed to this:
java.lang.ClassCastException: class org.hibernate.mapping.SingleTableSubclass cannot be cast to class org.hibernate.mapping.RootClass (org.hibernate.mapping.SingleTableSubclass and org.hibernate.mapping.RootClass are in unnamed module of loader 'app')
at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:214)
at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:205)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2283)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:976)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:803)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:254)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:230)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:273)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:903)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:934)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at main.Main.main(Main.java:47)
Exception in thread "main" java.lang.NullPointerException
at main.Main.main(Main.java:64)
专注分享java语言的经验与见解,让所有开发者获益!
评论