onetomany unidirectional with jointable setup using jpa(onetomany unidirectional with jointable setup using jpa)
我有两个实体,即客户关系和订单关系。 一个客户可以拥有多个订单。 因为我需要这种关系是单向的,所以我使用的是joinTable。
我可以使用JPA向我的客户实体添加条目。 我可以使用JPA向我的订单实体添加条目。
我想知道如何将两者连接在一起的数据。 假设我在客户表中有一个条目,在订单表中有两个条目。 我想将order表中的这两个条目与customer表中的一个条目相关联。
目前,我在jointable customer_order中没有看到任何条目。 我如何建立关联? 我想当我将订单添加到订单表中时,我将不得不以某种方式提及客户ID号。 不知道该怎么做。 在JPA中是否有标准查询?
谢谢。
客户类 -
@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......... */ }
订单类 -
@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......... */ }
客户表说明:
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)
订单表说明:
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联合表描述:
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)
我可以在customer表中插入一个项目:
jbossql=# select * from customer; customer_id | name -------------+------------- 1 | joe (1 row)
我也可以将项目插入到oder表中:
jbossql=# select * from order; order_id | value ----------+----------- 1 | 1.8 2 | 0.5 (2 rows)
我以为customer_order表会自动填充,即hibernate会采取这种方式。 但似乎不是因为我的连接表是空的:
jbossql=# select * from customer_order; customer_id | order_id -------------+----------- (0 rows)
所以,我的目的是让这两个订单条目与客户乔相关联。
请帮忙。
- 我是否必须向customer_order表明确添加记录?
- 如果没有,我如何将项目插入订单表,以便我可以将该条目连接到特定客户?
- 我的映射是否正确在这个OneToMany [unidirection]关系的java文件中按预期工作
- 我正在使用JPA2和JBoss以及Hibernate。 你有任何代码参考TEST一对多的关系吗? 对完整项目或阅读材料的任何参考都会有所帮助。
谢谢你的期待。
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.
原文:https://stackoverflow.com/questions/15374334
最满意答案
创建列表后添加一秒钟的睡眠。 页面不存在,因为列表尚未完成创建。
# create list and return list id list_id = twitter.create_list(name=user)['id'] print list_id time.sleep(1) # Sleep one second # get users following ids following_ids = twitter.get_friends_ids(screen_name=user)['ids'] print following_ids for i in chunks(following_ids, 100): # add memebers to list - 100 at a time twitter.create_list_members(list_id=list_id, user_id=i)
Add a sleep of one second after creating the list. The page doesn't exist because the list hasn't finished being created.
# create list and return list id list_id = twitter.create_list(name=user)['id'] print list_id time.sleep(1) # Sleep one second # get users following ids following_ids = twitter.get_friends_ids(screen_name=user)['ids'] print following_ids for i in chunks(following_ids, 100): # add memebers to list - 100 at a time twitter.create_list_members(list_id=list_id, user_id=i)
相关问答
更多-
404寻找〜/账号/登录(404 looking for ~/Account/LogOn)[2022-02-21]
好吧,没关系,我想通了。 对于我的路线登记,我想我应该指定该区域作为路线的一部分; 这显然是不正确的。 一旦我删除了路线登记的area段,所有都正常工作。 OK, never mind, I figured it out. for my route registration, I thought I should specify the area as part of the route; this was apparently incorrect. Once I removed the area segm ... -
我该怎么抓404?(How can I catch a 404?)[2022-01-25]
使用HttpStatusCode Enumeration ,特别是HttpStatusCode.NotFound 就像是: HttpWebResponse errorResponse = we.Response as HttpWebResponse; if (errorResponse.StatusCode == HttpStatusCode.NotFound) { // } 哪里 we是一个WebException 。 Use the HttpStatusCode Enumeration, spec ... -
创建列表后添加一秒钟的睡眠。 页面不存在,因为列表尚未完成创建。 # create list and return list id list_id = twitter.create_list(name=user)['id'] print list_id time.sleep(1) # Sleep one second # get users following ids following_ids = twitter.get_friends_ids(screen_name=user)['ids'] p ...
-
关于弹簧安全的404错误(404 error on spring security)[2023-11-27]
您需要允许访问favicon的所有内容。 :) You need to allow access to all for the favicon. :) -
REST给404(REST giving 404)[2022-01-07]
对不起,这是我犯的一个非常愚蠢的错误,我从来没有注意到。 在更改项目时,我必须更新包中的条目jersey.config.server.provider.packages com.javatpoint.rest -
您将控制器定义为@RestController ,它在访问secure/list页面时会导致错误。 我的建议是使用@Controller注释而不是@RestController创建另一个控制器。 @Controller public class SecureListController { @RequestMapping(value={"/secure/list"}, method=RequestMethod.GET) public ModelAndView getSecureList() ...
-
404找不到请求资源(404 Request Resource not found)[2023-07-02]
发生此错误是因为您忘记添加 @RequestMapping(value = "/{name}", method = RequestMethod.GET) 在找到方法之前: @RestController @RequestMapping("/test") public class AreaController { @RequestMapping(value = "/{name}", method = RequestMethod.GET) public RestResponse find(@P ... -
获得带有$ http帖子的404(getting 404 with $http post)[2022-11-28]
尝试 result.resource_server_base_uri = unescape(query_string.resource_server_base_uri); 我相信/它实际上是一个分隔符,但是%2f变成了一个普通的角色,只代表你的网址中的/字符。 unescape()已被弃用; 尝试decodeURIComponent() Try result.resource_server_base_uri = unescape(query_string.resource_server_base_uri) ... -
Twitter API / Twython - 显示用户获取用户个人资料图片(Twitter API/Twython - show user to get user profile image)[2022-02-12]
您需要使用screen_name参数调用show_user方法 t = Twython(app_key=settings.TWITTER_CONSUMER_KEY, app_secret=settings.TWITTER_CONSUMER_SECRET, oauth_token=oauth_token, oauth_token_secret=oauth_token_secret) print t.show_user(screen_na ... -
应该中止(404);(Should abort(404); be returned?)[2022-08-10]
如果找不到模型, firstOrFail()将抛出错误。 如果你想抓住它,我建议你在global.php中做 哟不需要返回abort() ,如文档中所示 firstOrFail() will throw an error if a model is not found. If you want to catch it I would suggest doing it in the global.php Yo do not need to return the abort() as shown in the ...