Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JPA外键不作为对象_Spring_Hibernate_Jpa - Fatal编程技术网

Spring JPA外键不作为对象

Spring JPA外键不作为对象,spring,hibernate,jpa,Spring,Hibernate,Jpa,我在MySQL中有两个简单的表 User ---(one-to-many)--- Expense 我正在使用SpringJPA生成这两个实体。费用具有“用户id”外键字段,该字段引用用户表中的“id”字段 我在请求中使用JSON来填充表 User {"id":1, "username":"user"} Expense { "id":1, "user_id":1, <--- this value is not null in MySQL "expense":"exp

我在MySQL中有两个简单的表

User ---(one-to-many)--- Expense
我正在使用SpringJPA生成这两个实体。费用具有“用户id”外键字段,该字段引用用户表中的“id”字段

我在请求中使用JSON来填充表

User
{"id":1, "username":"user"}

Expense
{
 "id":1, 
 "user_id":1,        <--- this value is not null in MySQL
 "expense":"expense"
}

您不应该在请求中传输JPA实体

为要执行的功能创建具有必要ID的POJO,并映射服务层中的java实体

例如,对于查询,您只需实例化实体并设置ID,如果需要更多字段,则可以按ID进行查找(),并通过直接访问对象来获取其余属性

@Entity
public class User{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private int id;

    private String username; 
}

@Entity
public class Car{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private int id;

    @ManyToOne
    private User user_id;

    private String expense;
}