Eclipse + JBoss 5 + EJB3 Development Guide (8)

Source: Internet
Author: User
Tags jboss

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

EJB3 's One-to-many mappings are set using @onetomany, and if it is a two-way one-to-many mapping, the @manytoone setting is required on the many side. Given two tables in this book, the other table t_customers in the previous article, and the structure of the other table is shown in Figure 1.

Figure 1 T_orders Table

T_customers and T_orders tables are a one-to-many relationship, a customer may have multiple order, and an order can only have one customer.

You need to define a property of a collection type in the customer class to hold multiple order objects, and the customer class code is 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;

        @OneToMany (Mappedby = "Customer", cascade = cascadetype.all) public collection<order> getorders () {

    return orders;

    public void Setorders (collection<order> orders) {this.orders = orders; } @Id @GeneRatedvalue (strategy = generationtype.identity) public int getId () {return id; }     ... ... }

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.