复制@ElementCollection行为

时间:2014-10-13 04:48:48

标签: java hibernate jpa orm

无论如何我可以在没有实际注释的情况下获得@ElementCollection的效果吗?我正在使用Hibernate 3.3,而@ElementCollection和@CollectionTable仅支持Hibernate 3.5及更高版本。但我真的需要使用这些注释,例如:

http://www.concretepage.com/hibernate/elementcollection_hibernate_annotation (我们得到字符串列表而不是完整实体的列表)

1 个答案:

答案 0 :(得分:1)

您可以使用<element>标记执行相同的操作,请参阅hibernate文档中的以下链接:

7.2.3. Collections of basic types and embeddable objects

链接中给出的示例是:

<element
        column="column_name"                               (1)
        formula="any SQL expression"                       (2)
        type="typename"                                    (3)
        length="L"
        precision="P"
        scale="S"
        not-null="true|false"
        unique="true|false"
        node="element-name"
/>

1   column (optional): the name of the column holding the collection element values.
2   formula (optional): an SQL formula used to evaluate the element.
3   type (required): the type of the collection element.

请参阅此链接以获取示例: Collection Mapping

<强> Star.java

private Set<String> planets = new HashSet<String>();

<强> Star.hbm.xml

<set name="planets" table="star_planet">
    <key column="star_id" />
    <element type="text"/>
</set>

<强>更新

您必须为给定的实体类使用xml映射或注释,但不能同时使用两者。

如果您只想使用注释查看示例,那么如果您在Google中进行搜索,则可以使用这些示例,如果您在实施它们时遇到问题,请检查并告知我们。

最后,是的,它适用于字符串集,整数或长等。

<强>更新

这是一个简单的示例,展示了如何使用元素集合:

<强> User.java

import java.util.Arrays;
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.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "TB_User")
public class User {
    @Id
    @GeneratedValue
    private int id;
    private String name;

    @ElementCollection
    @CollectionTable(name = "Addresses", joinColumns = @JoinColumn(name = "user_id"))
    @AttributeOverrides({ @AttributeOverride(name = "street1", column = @Column(name = "fld_street")) })
    public Set<Address> addresses = new HashSet<Address>();

    public User() {
    }

    public User(String name, Address... addresses){
        this.name = name;
        this.addresses.addAll(Arrays.asList(addresses));
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Set<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }   
}

<强> Address.java

import javax.persistence.Embeddable;

@Embeddable
public class Address {
    private String street1;

    public Address() {
    }

    public Address(String street1) {
        this.street1 = street1;
    }

    public String getStreet1() {
        return street1;
    }

    public void setStreet1(String street1) {
        this.street1 = street1;
    }

    @Override
    public String toString() {
        return street1;
    }
}

测试这个的简单逻辑:

private static void showUsers() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.getTransaction().begin();
    List<User> users = session.createQuery("from User").list();

    for (User user : users) {
        System.out.println(user.getName() + " -- > " + user.getAddresses());
    }

    session.getTransaction().commit();
}

private static void saveUsers() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.getTransaction().begin();
    User user1 = new User("U1", new Address("A1"), new Address("A11"));
    User user2 = new User("U2", new Address("A2"));
    session.save(user1);
    session.save(user2);
    session.getTransaction().commit();
}