JPA 1.0 Hibernate 和 JPA 1.0具有 Enum 键用法的 Derby HashMap

发布于 2025-01-08 09:00:13 字数 2872 浏览 1 评论 0原文

我正在寻找使用 JPA 映射 HashMap 的帮助,它的构建方式如下..。 NflTeam 是所有可能的 NFL 球队的枚举。在此阶段,地图的价值并不重要。

我尝试过,不在实体类中的哈希映射字段上使用任何注释,但我发现当我尝试更新映射时,出现以下错误:

错误 org.hibernate.util.JDBCExceptionReporter - 截断错误 尝试收缩 VARCHAR () FOR BIT DATA '(二进制数据 值未显示)' 长度为 255。 5371 [AWT-EventQueue-0] 错误 org.hibernate.event.def.AbstractFlushingEventListener - 无法 将数据库状态与会话同步

我还尝试在实体类@ElementCollection中的字段中添加以下注释,因为我看到一些地方说这是用于映射哈希映射的注释,但我发现当我尝试映射实体时出现以下错误:

引起:org.hibernate.AnnotationException:非法尝试映射 非集合作为 @OneToMany、@ManyToMany 或 @CollectionOfElements: 即.madden.stats.core.beans.Player.teamsPlayedWithMap

有谁知道在 JPA 中使用的正确注释来映射通用哈希图(由 构造)?

预先感谢


编辑::

这是我的 persistence.xml 文件:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">  <persistence-unit name="MaddenStatsPU" transaction-type="RESOURCE_LOCAL">    <provider>org.hibernate.ejb.HibernatePersistence</provider>    <class>ie.madden.stats.core.beans.Player</class>    <class>ie.madden.stats.core.beans.Fixture</class>    <properties>
     <property name="hibernate.show_sql" value="true"/>
     <property name="hibernate.format_sql" value="false"/>
     <property name="javax.persistence.jdbc.url" value="jdbc:derby:maddenstats;create=true"/>
     <property name="javax.persistence.jdbc.password" value="madden"/>
     <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
     <property name="javax.persistence.jdbc.user" value="madden"/>
     <property name="hibernate.hbm2ddl.auto" value="update"/>
     <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
     <property name="hibernate.default_schema" value="MaddenStats"/>    </properties>  </persistence-unit> </persistence>

其次,这是我的 Player 实体类,其中使用了哈希图:

@Entity
public class Player implements IEntityBean
{
    @Id
    @GeneratedValue(generator="id_Gen")
    @SequenceGenerator(name="id_Gen", sequenceName="id_Seq")
    private Long playerId;

    private String name;
    private Long played = new Long(0);
    private Long wins = new Long(0);
    private Long losses = new Long(0);
    private Integer winPercentage = new Integer(0);

    private HashMap<NflTeam, Integer> teamsPlayedWithMap = new HashMap<>();
}

在上面的示例中,我已删除我为 HashMap 的映射所做的任何尝试。

哦,NflTeam 也是一个枚举,就像我已经说过的那样,它是所有 NFL 球队的枚举......

I am looking for help in mapping a HashMap, using JPA, which is built like so.. <NflTeam, Integer>. NflTeam is an enum of all the possible NFL Teams. The value for the map is unimportant at this stage.

I have tried, not using any annotation over the hash map field in the entity class, but what I have found is that when I attempt to update the map, I get the following error:

ERROR org.hibernate.util.JDBCExceptionReporter - A truncation error
was encountered trying to shrink VARCHAR () FOR BIT DATA '(Binary data
value not displayed)' to length 255. 5371 [AWT-EventQueue-0] ERROR
org.hibernate.event.def.AbstractFlushingEventListener - Could not
synchronize database state with session

I have also tried to add the following annotation to the field in the entity class @ElementCollection, as I have seen a few places say that this is the annotation use to map hash maps, but I am finding I get the following error when I attempt to map the entity:

Caused by: org.hibernate.AnnotationException: Illegal attempt to map a
non collection as a @OneToMany, @ManyToMany or @CollectionOfElements:
ie.madden.stats.core.beans.Player.teamsPlayedWithMap

Does anyone know the correct annotation to use in JPA, to map a genericized hashmap (constructed of an )?

Thanks in advance


EDIT::

Here is my persistence.xml file:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">  <persistence-unit name="MaddenStatsPU" transaction-type="RESOURCE_LOCAL">    <provider>org.hibernate.ejb.HibernatePersistence</provider>    <class>ie.madden.stats.core.beans.Player</class>    <class>ie.madden.stats.core.beans.Fixture</class>    <properties>
     <property name="hibernate.show_sql" value="true"/>
     <property name="hibernate.format_sql" value="false"/>
     <property name="javax.persistence.jdbc.url" value="jdbc:derby:maddenstats;create=true"/>
     <property name="javax.persistence.jdbc.password" value="madden"/>
     <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
     <property name="javax.persistence.jdbc.user" value="madden"/>
     <property name="hibernate.hbm2ddl.auto" value="update"/>
     <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
     <property name="hibernate.default_schema" value="MaddenStats"/>    </properties>  </persistence-unit> </persistence>

Secondly here is my entity class for Player where the hashmap is being used:

@Entity
public class Player implements IEntityBean
{
    @Id
    @GeneratedValue(generator="id_Gen")
    @SequenceGenerator(name="id_Gen", sequenceName="id_Seq")
    private Long playerId;

    private String name;
    private Long played = new Long(0);
    private Long wins = new Long(0);
    private Long losses = new Long(0);
    private Integer winPercentage = new Integer(0);

    private HashMap<NflTeam, Integer> teamsPlayedWithMap = new HashMap<>();
}

In the sample above I have removed any attempts I have tried for the mapping of the HashMap.

Oh also NflTeam is an enum, which like I have already said is a enum of all the NFL Teams...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2025-01-15 09:00:13

JPA1提供对此类映射的支持。您必须了解实施细节。 JPA2 可以。

JPA1 does not provide support for such a Map. You have to go to implementation specifics. JPA2 does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文