onetomany unidirectional with jointable setup usin

2019-07-03 17:04发布

I have two entities namely customer and order in a onetomany relationship. One customer can have multiple orders. Since i needed this relationship to be unidirectional, i am using a joinTable.

I am able to add entries to my customer entity using JPA. I am able to add entries to my order entity using JPA.

I am wondering how to connect the two together data. Let's say i have one entry in customer table and two entries in order table. I would like to associate these two entries in order table to the one entry in customer table.

Currently, i don't see any entries in my jointable customer_order. How do i make the association? I guess while i am adding the orders into the order table, i will have to mention the customer id number somehow. Not sure how to do that. Is there a criteria query for that in JPA?

Thanks.

Customer Class -

@Entity
public class Customer implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorCustomer")
    @TableGenerator(name = "generatorCustomer", allocationSize = 1)
    @Column(name="customer_id")
    private Long id;
    public Long getId() {
        return id;
    }

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

    @OneToMany()
    @JoinTable (
        name="customer_order",
        joinColumns={ @JoinColumn(name="customer_id", referencedColumnName="customer_id") },
        inverseJoinColumns={ @JoinColumn(name="order_id", referencedColumnName="order_id", unique=true) }
    )
    private List<Order> orderList = new ArrayList<Order>();
    public List<Order> getOrderList() {
        if(this.orderList == null) {
            this.orderList = new ArrayList<Order>();
        }
        return this.orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    public void addOrder(Order order) {
        this.orderList.add(order);
    }

    /* other logic follows......... */
}

Order Class -

@Entity
public class Order implements Serializable {
@Id
@GeneratedValue
@Column(name="order_id")
private Long id;
public Long getId() {
    return id;
}

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

/* other logic follows......... */
}

Customer table description:

jbossql=# \d customer
Column   |         Type          |                        Modifiers
-------------+-----------------------+---------------------------------------------------------------
customer_id | bigint                | not null default nextval('customer_customer_id_seq'::regclass)
name        | character varying(50) | 
Indexes:
"customer_pkey" PRIMARY KEY, btree (customer_id)
Referenced by:
TABLE "customer_order" CONSTRAINT "fk8ef2f420ec016855" FOREIGN KEY (customer_id) REFERENCES customer(customer_id)

Order Table description:

jbossql=# \d order
Column   |  Type  | Modifiers 
------------+--------+-----------
order_id   | bigint | not null
value      | real   | 
Indexes:
"order_pkey" PRIMARY KEY, btree (order_id)
Referenced by:
TABLE "customer_order" CONSTRAINT "fk1e4828a98187660" FOREIGN KEY (order_id) REFERENCES order(order_id)

customer_order joint table description:

jbossql=# \d customer_order
Column       |  Type  | Modifiers 
--------------+--------+-----------
customer_id  | bigint | not null
order_id     | bigint | not null
Indexes:
"customer_order_order_id_key" UNIQUE CONSTRAINT, btree (order_id)
Foreign-key constraints:
"fk1e4828a98187660" FOREIGN KEY (order_id) REFERENCES order(order_id)
"fk1e4828adba386b8" FOREIGN KEY (customer_id) REFERENCES customer(customer_id)

I am able to insert an item into the customer table:

jbossql=# select * from customer;
customer_id | name 
-------------+-------------
1        | joe
(1 row)

I am able to insert items into the oder table as well:

jbossql=# select * from order;
order_id | value 
----------+-----------
1       |  1.8
2       |  0.5
(2 rows)

I was thinking the customer_order table would automatically get populated i.e hibernate would take of that. But appears not because my jointable is empty:

jbossql=# select * from customer_order;
customer_id | order_id 
-------------+-----------
(0 rows)

So, my intention is to make those two order entries connected to customer joe.

Please help.

  1. Do i have to explicitly add a record to the customer_order table?
  2. If not, how do i insert items into order table so that i can connect that entry to a particular customer?
  3. Are my mappings right in the java files for this OneToMany [unidirection] relationship to work as expected
  4. I am using JPA2 and JBoss and Hibernate. Do you have any code references to TEST a one-to-many relationship? Any references for a complete project or reading material would help.

Thanks for looking.

2条回答
狗以群分
2楼-- · 2019-07-03 17:11

You have to add the cascade attribute to the @OneToMany annotation: @OneToMany(cascade=CascadeType.ALL)

You can see here all types of operations available for cascade: http://docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html

And here you can read more about the cascade attribute: http://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html

You will not need to explicitly insert into customer_order.

查看更多
我只想做你的唯一
3楼-- · 2019-07-03 17:16

It's extremely simple:

customer.addOrder(order);

That's all you need. That's the principle of an ORM. You manipulate objects, and the ORM saves them in the database using the mapping you defined.

查看更多
登录 后发表回答