Eclipse + JBoss 5 + EJB3 Development Guide (9)

Source: Internet
Author: User
Tags jboss

Implement Many-to-many (Many-to-many) mappings for entity beans

You need to use @manytomany to annotate fields or getter methods that encapsulate many-to-many relationships in EJB3. First look at the following table:

Figure 1 T_addresses Table

The T_addresses table and the T_customers table are many-to-many relationships. You need to use an association table to describe this relationship, and the structure of the associated table is shown in the following illustration.

Figure 2 T_customers_addresses

Define a Collection<address> Type field (addresses) in the customer class to hold multiple address objects corresponding to the Customer object, as follows:

Package entity;

Import java.util.Collection;

Import Javax.persistence.CascadeType;

Import javax.persistence.Entity;

Import Javax.persistence.FetchType;

Import Javax.persistence.GeneratedValue;

Import Javax.persistence.GenerationType;

Import Javax.persistence.Id;

Import Javax.persistence.JoinColumn;

Import javax.persistence.JoinTable;

Import Javax.persistence.ManyToMany;

Import Javax.persistence.OneToMany;

Import Javax.persistence.OneToOne;

Import Javax.persistence.PrimaryKeyJoinColumn;

Import javax.persistence.Table;

    @Entity @Table (name = "T_customers") public class Customer {private int id;

    private String name;

    Private referee Referee;

    Private collection<order> orders;

    private collection<address> addresses; @ManyToMany (cascade = cascadetype.persist, fetch = Fetchtype.lazy) @JoinTable (name = "T_customers_addresses", Joincol Umns = @JoinColumn (name = "Customer_ID", referencedcolumnname = "id"), inversejoincolumns = @JoinColumn (name = "address_id", referencedcolumnname = "id")) public collection<address> getaddresses ()

    {return addresses; }     ... ... }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.