如何在Entity中同时使用@Embadable和@CollectionOfElements

时间:2014-03-13 05:26:54

标签: java hibernate hibernate-mapping

嗨,任何人都可以帮我解决这个问题,我有一个叫做人的实体,而且我已经把人状态(实例变量)的人名称作为字符串类型,人物名字作为Set类型,personphonnumbers作为Set类型( Phone是一个类型嵌入式),我想在这里使用personAddress,Address是一个类类型(Embeddable),我希望通过使用@AttributeOverride注释和@Embedded在我的person类中覆盖的地址统计(实例变量),但是如果我覆盖@AttributeOverride注释和@Embedded我的所有Set属性(petnames为Set,Set)将不起作用。如果在下面这样做,那么person类会询问(创建实体Id或创建子类),如果我没有使用Address属性,剩下的东西工作正常。

PersonEntity

import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.AttributeOverride;
    import javax.persistence.AttributeOverrides;
    import javax.persistence.CollectionTable;
    import javax.persistence.Column;
    import javax.persistence.ElementCollection;
    import javax.persistence.Embedded;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import org.hibernate.annotations.CollectionOfElements;

    /**
     *
     * @author hyva
     */
    @Entity
    public class Person implements Serializable {

        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String name;


        @CollectionOfElements
        @JoinTable(name = "PET", joinColumns = {
            @JoinColumn(name = "person_id")})
        @Column(name = "petname")
        private Set<String> petname12 = new HashSet<String>();

        public Set<PhoneNumber> getPhones() {
            return Phones;
        }

        public void setPhones(Set<PhoneNumber> Phones) {
            this.Phones = Phones;
        }
        @CollectionOfElements
        @JoinTable(name = "ponenumbers", joinColumns = {
            @JoinColumn(name = "person_id")})
        private Set<PhoneNumber> Phones = new HashSet<PhoneNumber>();

         @Embedded
        private Address homeAddress =new Address();
        @AttributeOverrides({
            @AttributeOverride(name = "street", column =
                    @Column(name = "street")),
            @AttributeOverride(name = "zip", column =
                    @Column(name = "zip")),
            @AttributeOverride(name = "country", column =
                    @Column(name = "country"))
        })

        public Long getId() {
            return id;
        }

        public Set<String> getPetname12() {
            return petname12;
        }

        public void setPetname12(Set<String> petname12) {
            this.petname12 = petname12;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean addPets(String p) {
            return petname12.add(p);
        }

        public boolean addPhones(PhoneNumber p) {
            return Phones.add(p);
        }


        @Override
        public String toString() {
            return "Person{" + "id=" + id + ", name=" + name + ", petname12=" + petname12 + '}';
        }
    }

地址类

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.io.Serializable;
import javax.persistence.Embeddable;

/**
 *
 * @author hyva
 */
@Embeddable
public class Address implements Serializable {

    private String street;
    private String zip;


    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

}

0 个答案:

没有答案