AngularJS + Spring JPA存储库多对一字段未保存 [英] Angularjs + Spring jpa repository Many-to-one field is not saving
问题描述
我正在使用Angularjs,Spring jpa存储库,休眠模式. 问题是无法在订单表中保存客户的ID". 表格名称:Order,CustomerGlobal 订单表中客户"的参考列名称为customerGlobal_id. 订单属于客户.订单表中有一个customerGlobal_id字段.
I am using Angularjs, Spring jpa repository, hibernate. The problem is "Unable to save the id of the Customer in the order table." table names: Order, CustomerGlobal reference col name for Customer in Order table is customerGlobal_id. Order belongs to a Customer. Order table has a customerGlobal_id field.
在实体(订单)中,我已经定义
In the Entity (Order), I have defined
@ManyToOne(optional = false) @JsonIgnore
CustomerGlobal customerGlobal;
订单属于一位客户.如果我指定一个JoinColumn,则hibernate会在数据库中生成该列.因此,我认为那里没有问题. 我为customerGlobal字段添加了Getter和Setter.
Order belongs to one Customer. If I specify a JoinColumn, hibernate is generating the column in the database. So, I think there is no issue there. I have added Getter and Setter for the customerGlobal field.
我正在使用JPA存储库,该接口的定义如下:
I am using JPA repository, the interface is defined as follows:
public interface OrderRepo extends JpaRepository<Order, Long>, JpaSpecificationExecutor<Order> {
...
}
我认为这也可以.
这是html的相关部分:
Here is the relevant part of the html:
<select ng-options="convertToInt(customer.id) as customer.name for customer in c" class="form-control " name="customerGlobal" ng-model="req.customerGlobal" required></select>
这是从关联控制器保存的表单的一部分. c的值是通过加载时的get请求获得的.
This is part of a form which gets saved from the associated controller. The values for c are obtained by get request on load.
[{"id":1,"name":"XYZ","contactPerson":"ABC","mobile":"1111111111","email":"abc@adef.com",}]
这似乎工作正常.我在表中获得了客户的下拉列表.
This seems to be working fine. I get a dropdown list of the customers in the table.
保存表单时,customerGlobal_id和其他字段值是通过POST发送的,但customerGlobal_id不会保存到数据库中.
When I save the form, customerGlobal_id and other field values are sent via POST but the customerGlobal_id does not get saved to the database.
这似乎不是一个非常具体的问题.这是一个基本的多对一关系,没有得到保存.我对Angularjs并不完全熟悉.所以请帮我解决这个问题.预先感谢.
This does not seem to be a very specific problem. This is a basic many-to-one relationship that is not getting saved. I am not completely familiar with Angularjs. So please help me out with this. Thanks in advance.
推荐答案
为了保存与Spring Rest的关系,您不应使用实体的ID,而应使用引用网址实体.假设您有一个Car
实体,该实体与Make
实体有关联,并且您想要设置汽车的品牌.这是您应该做的:
In order to save a relationship with spring rest, you shouldn't use the id of the entity, but the reference url of the entity. So let's say that you have a Car
entity that has an association with a Make
entity, and you want to set the make of the car. Here is what you should do:
第1步.获取Make
实体:
-get a specific `Make`: localhost:8080/makes/1
-get a a list of `Make`: localhost:8080/makes
,然后选择所需的一个:
and select the one you want:
{
"name" : "Toyota",
"country" : "Japan",
"_links" : {
"self" : {
"href" : "http://localhost:8080/makes/1"
}
}
第2步.现在像这样插入/更新Car
:
Step 2. Now insert / update a Car
like this:
POST/PUT http://localhost:8080/cars/123
POST/PUT http://localhost:8080/cars/123
{
"model" : "Corolla",
"year" : "2006",
"hp" : 95,
"make" : "http://localhost:8080/makes/1"
}
和不是这样的:
{
"model" : "Corolla",
"year" : "2006",
"hp" : 95,
"make" : 1
}
Spring Data Rest可以理解实体的引用网址.
Spring Data Rest understands the reference url of the entity.
这篇关于AngularJS + Spring JPA存储库多对一字段未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!