1

I use the annotation hibernate i have thise source

     @Table(name = "stats")
        public class Stat {

            @Id
            @GeneratedValue(generator = "increment")
            @GenericGenerator(name = "increment", strategy = "increment")
            private long id;

            @ManyToOne(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY)
            @JoinColumn(name = "player_id")
            private PPlayer player;

            @Column(name = "stat_type")
            private int type;

            @Column(name = "stat_value")
            private int value;

        }

    @Entity
    @Table(name = "Users")
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
        public class PPlayer {

            @Id
        @GeneratedValue(generator = "increment")
        @GenericGenerator(name = "increment", strategy = "increment")
        @Column(name = "user_id")
        private long id;

        private String username;
        private String email;
        private Date joiningDate;
        private String key;
        private String password;

        private int rank;
        private int score;

            @OneToMany(mappedBy = "player", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
                @MapKey(name = "type")
                private Map<Integer, Stat> stats;
        }

I watched a lot of documentation but I can not make out @MapKey

What means the name = "type"? why "type"? the class in which I can view it? and where i can found mappedBy = "player" ?

4

3 回答 3

1

mappedBy='player'指的是类的属性player——Stat它是关系的拥有方。

name = "type"指的是同一个类的属性type——Stat它表示 中的值Map被映射到不同类型的Stat.

编辑:注释中的值与表列名不对应可能会令人困惑。列名默认为字段名,但在您的实体中,列名被覆盖,使用@Column注释

Hibernate 和 JPA 对关系注释使用字段名,而不是列名,除非另有指定(如JoinColumn等)。

要记住的一个很好的启发式方法是 - 当注释中没有“列”时,您可以假设涉及字段名称。我不确定它是否适用于 100%,但在大多数情况下它确实适用。

于 2013-04-26T09:06:14.807 回答
0

您是否尝试过使用以下休眠手册:http ://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html

@MapKey尽管上面的手册使用 Map 集合处理 XML ORM 映射,但其基本原理与Hibernate 中的注释用法几乎相同。

于 2013-04-26T09:06:14.823 回答
-4

您可以查找MapKey的 JavaDoc 。

于 2013-04-26T08:56:34.653 回答