onetomany单向可连接设置使用jpa [英] onetomany unidirectional with jointable setup using jpa
问题描述
一个客户可以有多个订单。
由于我需要这种关系是单向的,我正在使用一个joinTable。
我可以使用JPA添加条目到我的客户实体。
我可以使用JPA向我的订单实体添加条目。
我想知道如何将两个数据连接在一起。
假设我在客户表中有一个条目,在订单表中有两个条目。
我想将这两个条目在订单表中关联到客户表中的一个条目。
目前,我没有看到我的可关联的条目客户订单。我如何进行协会?我猜,当我将订单添加到订单表中时,我将不得不提及客户编号。不知道该怎么做有JPA中的标准查询?
谢谢。
客户类 -
@Entity
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator =generatorCustomer)
@TableGenerator(name =generatorCustomer,allocateSize = 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参考ColumnName =customer_id)},
inverseJoinColumns = {@JoinColumn(name =order_id,referencedColumnName =order_id,unique = true)}
)
私人列表< 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);
}
/ *其他逻辑跟随......... * /
}
订单类 -
@Entity
public class Order implements可序列化{
@Id
@GeneratedValue
@Column(name =order_id)
private Long id;
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
/ *其他逻辑跟随......... * /
}
客户表描述:
jbossql =#\d customer
列|类型|修饰符
------------- + ----------------------- + ------- -------------------------------------------------- ------
customer_id | bigint | not null default nextval('customer_customer_id_seq':: regclass)
name |字符变化(50)|
索引:
customer_pkeyPRIMARY KEY,btree(customer_id)
参考:
TABLEcustomer_orderCONSTRAINTfk8ef2f420ec016855FOREIGN KEY(customer_id)REFERENCES customer(customer_id)
订单表描述:
jbossql =#\d order
列|类型|修饰符
------------ + -------- + -----------
order_id | bigint | not null
value |真实的|
索引:
order_pkeyPRIMARY KEY,btree(order_id)
引用:
TABLEcustomer_orderCONSTRAINTfk1e4828a98187660FOREIGN KEY(order_id)REFERENCES order(order_id)
customer_order联合表描述:
jbossql =#\d customer_order
列|类型|修饰符
-------------- + -------- + -----------
customer_id | bigint | not null
order_id | bigint | not null
索引:
customer_order_order_id_keyUNIQUE CONSTRAINT,btree(order_id)
外键约束:
fk1e4828a98187660FOREIGN KEY(order_id)REFERENCES order(order_id)
fk1e4828adba386b8FOREIGN KEY(customer_id)参考客户(customer_id)
我可以插入项目进入客户表:
jbossql =#select * from customer;
customer_id |名字
------------- + -------------
1 | joe
(1 row)
我能够将项目插入到表格中:
jbossql =#select * from order;
order_id |值
---------- + -----------
1 | 1.8
2 | 0.5
(2行)
我以为client_order表会自动填充,即hibernate将采取这一点。但似乎不是因为我的关节是空的:
jbossql =#select * from customer_order;
customer_id | order_id
------------- + -----------
(0行)
所以,我的目的是让这两个订单条目连接到客户joe。
请帮助
- 我必须向customer_order表格显式添加记录吗?
- 如果没有,我如何将项目插入到订单表中,以便我可以将该条目连接到特定的客户?
- 我的映射是否适合这个OneToMany [unidirection]关系的java文件正常工作
- 我正在使用JPA2和JBoss和Hibernate。你有任何代码参考TEST一对多关系吗?任何有关完整项目或阅读材料的参考资料都将有所帮助。
感谢您的查找。
非常简单:
customer.addOrder(order);
这就是你需要的。这是ORM的原则。您操纵对象,ORM使用您定义的映射将它们保存在数据库中。
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.
- Do i have to explicitly add a record to the customer_order table?
- If not, how do i insert items into order table so that i can connect that entry to a particular customer?
- Are my mappings right in the java files for this OneToMany [unidirection] relationship to work as expected
- 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.
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.
这篇关于onetomany单向可连接设置使用jpa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!