【发布时间】:2021-12-25 17:28:44
【问题描述】:
告诉我如何正确地将一个由信息对象组成的集合添加到数据库中(一个表,例如一个家庭),以便这些对象引用用户表中的一个用户。此刻出现错误,尝试了不同的保存选项,错误依旧:
请求json:
[
{
"name":"luda",
"surname":"Petrova",
"phone":"2353636",
"bus":"black"
},
{
"name":"dima",
"surname":"Petrov",
"phone":"23536336",
"bus":"red"
},
{
"name":"ivan",
"surname":"Petrov",
"phone":"2353",
"bus":"blue"
}
]
休息控制器:
@RequestMapping(value = { "fam/{id}" },method = RequestMethod.POST)
public User addyfam (@PathVariable Long id, @RequestBody List<FamReq> fammreq){
User usr = userRepo.findById(id).get();
var arr = new ArrayList<Family>();
for(FamReq f : fammreq ) {
arr.add(new Family( f.getName(),f.getSurname(),f.getPhone(),f.getBus()));
}
usr.setFamily(arr);
userRepo.save(usr);
return usr;
}
实体 1:
@Entity
@Table(name="usr")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
@OneToOne(
mappedBy = "user",
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY
)
@JsonManagedReference
private Passport passport;
@OneToMany(
mappedBy = "user",
cascade = {CascadeType.ALL},
orphanRemoval = true
)
private List<Family> family = new ArrayList<>();
/**get/set and constr **/
}
实体 2:
@Entity
@Table(name="family")
public class Family {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
@Column(name="phone")
private String phone;
@Column(name="bus")
private String bus;
@ManyToOne(
fetch = FetchType.LAZY
)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
private User user;
/**get/set and constr **/
}
由于主键标识与外键相同,因此family表只包含副键,user表只包含主键。
更新:
@RequestMapping(value = { "fam/{id}" },method = RequestMethod.POST)
public User addyfam (@PathVariable Long id, @RequestBody List<FamReq>
fammreq){
User us = userRepo.findById(id).get();
var arr = new ArrayList<Family>();
for(FamReq f : fammreq ) {
Family savedFamily = famRepo.save(new Family(f.getName(),f.getSurname(),f.getPhone(),f.getBus()));
arr.add(savedFamily);
Family(f.getName(),f.getSurname(),f.getPhone(),f.getBus()));
}
userRepo.save(arr);
return us;
}
如果从本质上讲,家庭表明了这一点:
@ManyToOne( cascade = CascadeType.ALL,
fetch = FetchType.LAZY
)
@JoinColumn(name="user_id", referencedColumnName = "id")
private User user;
错误:
ERROR: NULL in column "user_id" of relationship "family" violates NOT NULL constraint
Details: The error line contains (sdgsgsgsegf, luda, 2353636, Petrova, null, null)
如果您指出:
@ManyToOne(
fetch = FetchType.LAZY
)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
private User user;
然后
错误:
Error: attempted to assign id from null one-to-one property [com.dbtest.springboot.db_model.Family.user]
更新:
控制器当前正在使用中:
@RequestMapping(value = { "fam/{id}" },method = RequestMethod.POST)
public User addyfam (@PathVariable Long id, @RequestBody List<FamReq> fammreq){
User usr = userRepo.findById(id).get();
var arr = new ArrayList<Family>();
for(FamReq f : fammreq ) {
arr.add(new Family( f.getName(),f.getSurname(),f.getPhone(),f.getBus()));
}
usr.setFamily(arr);
userRepo.save(usr);
return usr;
}
回购:
@Repository
public interface UserRepo extends JpaRepository<User, Long >{
User findByname(String name);
void save(ArrayList<Family> fm);
}
完全错误:
Hibernate: insert into family (bus, name, phone, surname, user_id) values (?, ?, ?, ?, ?)
Hibernate: insert into family (bus, name, phone, surname, user_id) values (?, ?, ?, ?, ?)
Hibernate: insert into family (bus, name, phone, surname, user_id) values (?, ?, ?, ?, ?)
2021-11-14 23:31:06.693 ERROR 13192 --- [nio-9091-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.util.ArrayList]: Could not find field for property during fallback access!] with root cause
org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.util.ArrayList]: Could not find field for property during fallback access!
at org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.getPropertyValue(DirectFieldAccessFallbackBeanWrapper.java:58) ~[spring-data-commons-2.5.6.jar:2.5.6]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.getId(JpaMetamodelEntityInformation.java:162) ~[spring-data-jpa-2.5.6.jar:2.5.6]
at org.springframework.data.repository.core.support.AbstractEntityInformation.isNew(AbstractEntityInformation.java:46) ~[spring-data-commons-2.5.6.jar:2.5.6]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.isNew(JpaMetamodelEntityInformation.java:246) ~[spring-data-jpa-2.5.6.jar:2.5.6]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:596) ~[spring-data-jpa-2.5.6.jar:2.5.6]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
【问题讨论】:
-
请告诉我,解决问题需要很长时间,我不明白如何解决它
标签: java spring-boot hibernate spring-data-jpa