org.hibernate.AnnotationException: XML映射中未指定实体的标识符

huangapple 未分类评论64阅读模式
英文:

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>

但是,你遇到了一些问题,包括配置和注解。根据你提供的信息,我无法直接解决你的问题。你可能需要仔细检查以下几点:

  1. 确保你的类路径和包名设置正确。
  2. 检查类和属性的拼写是否正确。
  3. 确保所有需要的依赖库都已添加到项目中。
  4. 检查注解和映射是否正确配置。
  5. 确保没有重复映射或重复添加类到持久化单元。

如果你已经按照上述步骤操作并仍然遇到问题,可能需要详细检查错误日志以获得更多信息,以便确定问题所在。另外,考虑尝试不同的配置选项以解决问题。

英文:

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=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;entity-mappings xmlns=&quot;http://java.sun.com/xml/ns/persistence/orm&quot;
        version=&quot;2.0&quot;&gt;
    &lt;named-query name=&quot;Reservierung.findAll&quot;&gt;
        &lt;query&gt;SELECT b FROM model.Reservierung b&lt;/query&gt;
    &lt;/named-query&gt;
    &lt;entity name=&quot;Reservierung&quot; class=&quot;model.Reservierung&quot; &gt;
        &lt;table name=&quot;Reservierung&quot; /&gt;
        &lt;attributes&gt;
            &lt;id name=&quot;ID&quot;&gt;
                &lt;generated-value strategy=&quot;AUTO&quot; /&gt;
            &lt;/id&gt;
            &lt;basic name=&quot;datum&quot; /&gt;
            &lt;basic name=&quot;praemienMeilenBonus&quot; /&gt;
            &lt;basic name=&quot;preis&quot; /&gt;
            &lt;basic name=&quot;StatusInfo&quot; /&gt;

            &lt;one-to-one name=&quot;strecke&quot; /&gt;
            &lt;one-to-one name=&quot;zug&quot;/&gt;
            &lt;one-to-one name=&quot;benutzer&quot;/&gt;
            &lt;transient name=&quot;zahlung&quot;/&gt;
        &lt;/attributes&gt;
    &lt;/entity&gt;
&lt;/entity-mappings&gt;

finnaly I added the orm.xml and all the other files in my persistence.xml, which looks now like this

&lt;persistence xmlns=&quot;http://xmlns.jcp.org/xml/ns/persistence&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd&quot;
  version=&quot;2.1&quot;&gt;

  &lt;persistence-unit name=&quot;westbahn&quot;&gt;
      &lt;description&gt; Hibernate JPA Configuration Example&lt;/description&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;

      &lt;mapping-file&gt;META-INF/orm.xml&lt;/mapping-file&gt;

      &lt;class&gt;model.Bahnhof&lt;/class&gt;
      &lt;class&gt;model.Benutzer&lt;/class&gt;
      &lt;class&gt;model.Reservierung&lt;/class&gt;
      &lt;class&gt;model.Einzelticket&lt;/class&gt;
      &lt;class&gt;model.Preisstaffelung&lt;/class&gt;
      &lt;class&gt;model.Sonderangebot&lt;/class&gt;
      &lt;class&gt;model.Strecke&lt;/class&gt;
      &lt;class&gt;model.Ticket&lt;/class&gt;
      &lt;class&gt;model.Zeitkarte&lt;/class&gt;
      &lt;class&gt;model.Zug&lt;/class&gt;
      &lt;class&gt;model.Reservierung&lt;/class&gt;

        &lt;properties&gt;
    &lt;!--
          &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;com.mysql.cj.jdbc.Driver&quot; /&gt;
          &lt;property name=&quot;javax.persistence.jdbc.url&quot; value=&quot;jdbc:mysql://localhost:3306/westbahn?serverTimezone=UTC&quot; /&gt;
          &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;westbahnUser&quot; /&gt;
          &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;westbahnPassword&quot; /&gt;
    --&gt;
            &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;org.h2.Driver&quot; /&gt;
            &lt;property name=&quot;javax.persistence.jdbc.url&quot;    value=&quot;jdbc:h2:file:./db/testing&quot; /&gt;
            &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;sa&quot; /&gt;
            &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;&quot; /&gt;
            &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.H2Dialect&quot;/&gt;
            &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;false&quot; /&gt;
            &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;create-drop&quot; /&gt;
            &lt;!--&lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.HSQLDialect&quot;/&gt;--&gt;
        &lt;/properties&gt;
  &lt;/persistence-unit&gt;
&lt;/persistence&gt;

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


&lt;?xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot;?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC
        &quot;-//Hibernate/Hibernate Mapping DTD//EN&quot;
        &quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;

&lt;hibernate-mapping&gt;
    &lt;class name=&quot;model.Reservierung&quot; table=&quot;Reservierung&quot;&gt;
        &lt;meta attribute = &quot;class-description&quot;&gt;
            This class contains the reservations
        &lt;/meta&gt;

        &lt;id name = &quot;ID&quot; type = &quot;long&quot; column = &quot;ID&quot;&gt;
            &lt;generator class=&quot;native&quot;/&gt;
        &lt;/id&gt;

        &lt;property name = &quot;datum&quot; column = &quot;datum&quot; type = &quot;date&quot;&gt; &lt;/property&gt;
        &lt;property name = &quot;praemienMeilenBonus&quot; column = &quot;praemienMeilenBonus&quot;&gt; &lt;/property&gt;
        &lt;property name = &quot;preis&quot; column = &quot;preis&quot;&gt; &lt;/property&gt;
        &lt;property name = &quot;status&quot; column = &quot;status&quot;&gt;
            &lt;type name=&quot;org.hibernate.type.EnumType&quot;&gt;
                &lt;param name=&quot;enumClass&quot;&gt;model.StatusInfo&lt;/param&gt;
            &lt;/type&gt;
        &lt;/property&gt;
        &lt;one-to-one name=&quot;strecke&quot; /&gt;
        &lt;one-to-one name=&quot;zug&quot;/&gt;
        &lt;one-to-one name=&quot;benutzer&quot;/&gt;

    &lt;/class&gt;
&lt;/hibernate-mapping&gt;

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 &#39;app&#39;)
	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 &quot;main&quot; java.lang.NullPointerException
	at main.Main.main(Main.java:64)

huangapple
  • 本文由 发表于 2020年6月5日 21:33:05
  • 转载请务必保留本文链接:https://java.coder-hub.com/62216549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定