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

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

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:

  1. 我正在处理一个JPA程序并且需要通过xml映射为一个类编写注解我将我的模型类编写如下
  2. // Java代码
  3. ppackage model;
  4. import javax.persistence.*;
  5. import java.util.Date;
  6. public class Reservierung {
  7. public Reservierung(Date datum, int praemienMeilenBonus, int preis, StatusInfo status, Zug zug, Strecke strecke, Benutzer benutzer, Zahlung zahlung) {
  8. // 构造函数内容...
  9. }
  10. // 属性和方法...
  11. }

之后,我创建了名为orm的xml映射文件:

  1. // XML代码
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
  4. version="2.0">
  5. <!-- 映射定义... -->
  6. </entity-mappings>

最后,我将orm.xml和所有其他文件添加到了我的persistence.xml中,现在看起来像这样:

  1. // XML代码
  2. <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  5. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  6. version="2.1">
  7. <persistence-unit name="westbahn">
  8. <!-- 配置... -->
  9. </persistence-unit>
  10. </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:

  1. ppackage model;
  2. import javax.persistence.*;
  3. import java.util.Date;
  4. public class Reservierung {
  5. public Reservierung(Date datum, int praemienMeilenBonus, int preis, StatusInfo status, Zug zug, Strecke strecke, Benutzer benutzer, Zahlung zahlung) {
  6. this.datum = datum;
  7. this.praemienMeilenBonus = praemienMeilenBonus;
  8. this.preis = preis;
  9. this.status = status;
  10. this.zug = zug;
  11. this.strecke = strecke;
  12. this.benutzer = benutzer;
  13. this.zahlung = zahlung;
  14. }
  15. private Long ID;
  16. private Date datum;
  17. private int praemienMeilenBonus = 15;
  18. private int preis = 150;
  19. private StatusInfo status;
  20. private Zug zug;
  21. private Strecke strecke;
  22. private Benutzer benutzer;
  23. private Zahlung zahlung;
  24. public Date getDatum() {
  25. return datum;
  26. }
  27. public void setDatum(Date datum) {
  28. this.datum = datum;
  29. }
  30. public int getPraemienMeilenBonus() {
  31. return praemienMeilenBonus;
  32. }
  33. public void setPraemienMeilenBonus(int praemienMeilenBonus) {
  34. this.praemienMeilenBonus = praemienMeilenBonus;
  35. }
  36. public int getPreis() {
  37. return preis;
  38. }
  39. public void setPreis(int preis) {
  40. this.preis = preis;
  41. }
  42. public StatusInfo getStatus() {
  43. return status;
  44. }
  45. public void setStatus(StatusInfo status) {
  46. this.status = status;
  47. }
  48. public Zug getZug() {
  49. return zug;
  50. }
  51. public void setZug(Zug zug) {
  52. this.zug = zug;
  53. }
  54. public Strecke getStrecke() {
  55. return strecke;
  56. }
  57. public void setStrecke(Strecke strecke) {
  58. this.strecke = strecke;
  59. }
  60. public Benutzer getBenutzer() {
  61. return benutzer;
  62. }
  63. public void setBenutzer(Benutzer benutzer) {
  64. this.benutzer = benutzer;
  65. }
  66. public Zahlung getZahlung() {
  67. return zahlung;
  68. }
  69. public void setZahlung(Zahlung zahlung) {
  70. this.zahlung = zahlung;
  71. }
  72. public Long getID() {
  73. return ID;
  74. }
  75. public void setID(Long ID) {
  76. this.ID = ID;
  77. }
  78. }

and after that I created the xml mapping file that I named orm:

  1. ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;entity-mappings xmlns=&quot;http://java.sun.com/xml/ns/persistence/orm&quot;
  3. version=&quot;2.0&quot;&gt;
  4. &lt;named-query name=&quot;Reservierung.findAll&quot;&gt;
  5. &lt;query&gt;SELECT b FROM model.Reservierung b&lt;/query&gt;
  6. &lt;/named-query&gt;
  7. &lt;entity name=&quot;Reservierung&quot; class=&quot;model.Reservierung&quot; &gt;
  8. &lt;table name=&quot;Reservierung&quot; /&gt;
  9. &lt;attributes&gt;
  10. &lt;id name=&quot;ID&quot;&gt;
  11. &lt;generated-value strategy=&quot;AUTO&quot; /&gt;
  12. &lt;/id&gt;
  13. &lt;basic name=&quot;datum&quot; /&gt;
  14. &lt;basic name=&quot;praemienMeilenBonus&quot; /&gt;
  15. &lt;basic name=&quot;preis&quot; /&gt;
  16. &lt;basic name=&quot;StatusInfo&quot; /&gt;
  17. &lt;one-to-one name=&quot;strecke&quot; /&gt;
  18. &lt;one-to-one name=&quot;zug&quot;/&gt;
  19. &lt;one-to-one name=&quot;benutzer&quot;/&gt;
  20. &lt;transient name=&quot;zahlung&quot;/&gt;
  21. &lt;/attributes&gt;
  22. &lt;/entity&gt;
  23. &lt;/entity-mappings&gt;

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

  1. &lt;persistence xmlns=&quot;http://xmlns.jcp.org/xml/ns/persistence&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/persistence
  4. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd&quot;
  5. version=&quot;2.1&quot;&gt;
  6. &lt;persistence-unit name=&quot;westbahn&quot;&gt;
  7. &lt;description&gt; Hibernate JPA Configuration Example&lt;/description&gt;
  8. &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
  9. &lt;mapping-file&gt;META-INF/orm.xml&lt;/mapping-file&gt;
  10. &lt;class&gt;model.Bahnhof&lt;/class&gt;
  11. &lt;class&gt;model.Benutzer&lt;/class&gt;
  12. &lt;class&gt;model.Reservierung&lt;/class&gt;
  13. &lt;class&gt;model.Einzelticket&lt;/class&gt;
  14. &lt;class&gt;model.Preisstaffelung&lt;/class&gt;
  15. &lt;class&gt;model.Sonderangebot&lt;/class&gt;
  16. &lt;class&gt;model.Strecke&lt;/class&gt;
  17. &lt;class&gt;model.Ticket&lt;/class&gt;
  18. &lt;class&gt;model.Zeitkarte&lt;/class&gt;
  19. &lt;class&gt;model.Zug&lt;/class&gt;
  20. &lt;class&gt;model.Reservierung&lt;/class&gt;
  21. &lt;properties&gt;
  22. &lt;!--
  23. &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;com.mysql.cj.jdbc.Driver&quot; /&gt;
  24. &lt;property name=&quot;javax.persistence.jdbc.url&quot; value=&quot;jdbc:mysql://localhost:3306/westbahn?serverTimezone=UTC&quot; /&gt;
  25. &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;westbahnUser&quot; /&gt;
  26. &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;westbahnPassword&quot; /&gt;
  27. --&gt;
  28. &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;org.h2.Driver&quot; /&gt;
  29. &lt;property name=&quot;javax.persistence.jdbc.url&quot; value=&quot;jdbc:h2:file:./db/testing&quot; /&gt;
  30. &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;sa&quot; /&gt;
  31. &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;&quot; /&gt;
  32. &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.H2Dialect&quot;/&gt;
  33. &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;false&quot; /&gt;
  34. &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;create-drop&quot; /&gt;
  35. &lt;!--&lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.HSQLDialect&quot;/&gt;--&gt;
  36. &lt;/properties&gt;
  37. &lt;/persistence-unit&gt;
  38. &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

  1. &lt;?xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot;?&gt;
  2. &lt;!DOCTYPE hibernate-mapping PUBLIC
  3. &quot;-//Hibernate/Hibernate Mapping DTD//EN&quot;
  4. &quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;
  5. &lt;hibernate-mapping&gt;
  6. &lt;class name=&quot;model.Reservierung&quot; table=&quot;Reservierung&quot;&gt;
  7. &lt;meta attribute = &quot;class-description&quot;&gt;
  8. This class contains the reservations
  9. &lt;/meta&gt;
  10. &lt;id name = &quot;ID&quot; type = &quot;long&quot; column = &quot;ID&quot;&gt;
  11. &lt;generator class=&quot;native&quot;/&gt;
  12. &lt;/id&gt;
  13. &lt;property name = &quot;datum&quot; column = &quot;datum&quot; type = &quot;date&quot;&gt; &lt;/property&gt;
  14. &lt;property name = &quot;praemienMeilenBonus&quot; column = &quot;praemienMeilenBonus&quot;&gt; &lt;/property&gt;
  15. &lt;property name = &quot;preis&quot; column = &quot;preis&quot;&gt; &lt;/property&gt;
  16. &lt;property name = &quot;status&quot; column = &quot;status&quot;&gt;
  17. &lt;type name=&quot;org.hibernate.type.EnumType&quot;&gt;
  18. &lt;param name=&quot;enumClass&quot;&gt;model.StatusInfo&lt;/param&gt;
  19. &lt;/type&gt;
  20. &lt;/property&gt;
  21. &lt;one-to-one name=&quot;strecke&quot; /&gt;
  22. &lt;one-to-one name=&quot;zug&quot;/&gt;
  23. &lt;one-to-one name=&quot;benutzer&quot;/&gt;
  24. &lt;/class&gt;
  25. &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:

  1. 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;)
  2. at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:214)
  3. at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:205)
  4. at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2283)
  5. at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:976)
  6. at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:803)
  7. at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:254)
  8. at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:230)
  9. at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:273)
  10. at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:903)
  11. at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:934)
  12. at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
  13. at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
  14. at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
  15. at main.Main.main(Main.java:47)
  16. Exception in thread &quot;main&quot; java.lang.NullPointerException
  17. 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:

确定