在我的实际项目中,我使用角6作为前端开发,Spring 2用于后端开发。后端使用Spring连接到Postgres数据库,我还使用Spring创建API,使用Spring创建的默认端点。因此,没有手工制作的服务或控制器。
我有一个个人实体和一个地址实体,有一种单独的关系。所有实体都扩展了一个BaseEntity:
@Data
@MappedSuperclass
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@EntityListeners({AuditingEntityListener.class})
public class BaseEntity {
@Id
@GeneratedValue(generator = "uuid2", strategy = GenerationType.AUTO)
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Type(type = "uuid-binary")
private UUID id;
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
@CreatedBy
private String createdBy;
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedDate;
@LastModifiedBy
private String lastModifiedBy;
}人物
@Data
@Entity
@Table(name = "person")
public class Person extends BaseEntity {
private String lastName;
private String firstName;
}地址
@Data
@Entity
@Table(name = "address")
public class Address extends BaseEntity {
@OneToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@MapsId
private Person person;
private String street;
private String houseNumber;
private String zipCode;
private String city;
private String state;
}我的存储库如下所示:
public interface AddressRepository extends CrudRepository<Address, UUID> {
}和
public interface PersonRepository extends CrudRepository<Person, UUID> {
}张贴时
{
"street":"exStreet",
"housenumber":"121",
"zipcode":"14321",
"city":"exCity",
"state":"exState",
"person":{
"lastName":"ExampleLastName",
"firstName":"ExampleFirstName"}
}对于http://localhost:8080/addresses,我总是得到以下错误
org.springframework.orm.jpa.JpaSystemException:试图从空的一对一属性Address.person分配id;
与
org.hibernate.id.IdentifierGenerationException:试图从空的一对一属性Address.person分配id。
看起来服务器没有持久化person实体,尽管没有id来持久化地址实体。我努力做到这一点:
前端:使用显示的json => post到spring启动rest服务器// =>对httpclient的角形表单服务=>调用,我认为
后端: spring启动rest服务器,将json反序列化为adress,并通过jpa保存到数据库中的person实体=> address.setPerson(person) =>。
是否有办法通过对jackson或其他东西进行自定义来实现这一点,或者我是否必须再次编写服务--控制器,处理链接,.?我已经为此挣扎了两天,所以每一种帮助都是非常感谢的。
编辑经过进一步的调查,我敢肯定,问题是映射。但是,我不知道如何注释我的实体,也不知道需要做什么配置。
发布于 2018-10-27 14:03:17
好吧,我终于成功了。问题确实是地图问题。我实现了一个自定义反序列化器,并在Address实体上使用@JsonDeserialize(使用= AddressDeserializer.class)注册了它。见下文。
@Component
public class AddressDeserializer extends StdDeserializer<Address> {
public AddressDeserializer() {
super(Address.class);
}
@Override
public Address deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode addressNode = jsonParser.getCodec().readTree(jsonParser);
Person person = new Person();
person.setLastName(addressNode.get("person").get("lastName").textValue());
person.setFirstName(addressNode.get("person").get("firstName").textValue());
System.out.println("##### ----- person.getLastName: " + person.getLastName());
Address address = new Address();
address.setPerson(person);
address.setStreet(addressNode.get("street").textValue());
address.setHouseNumber(addressNode.get("housenumber").textValue());
address.setZipCode(addressNode.get("zipcode").textValue());
address.setCity(addressNode.get("city").textValue());
address.setState(addressNode.get("state").textValue());
return address;
}
}地址-实体:
...
@JsonDeserialize(using = AddressDeserializer.class)
public class Address extends BaseEntity implements Serializable {
...IntelliJ IDEA内置HTTP: POST http://localhost:8080/addresses内容-类型: application/hal+json接受:/Control:无缓存
{
"street":"exStreet",
"housenumber":"121",
"zipcode":"14321",
"city":"exCity",
"state":"exState",
"person":{
"lastName":"ExampleLastName",
"firstName":"ExampleFirstName"}
}结果与结果地址:
POST http://localhost:8080/addresses
HTTP/1.1 201
Last-Modified: Sat, 27 Oct 2018 13:50:16 GMT
Location: http://localhost:8080/addresses/89f57064-b4b4-4e1a-adae-142d8c09d834
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 27 Oct 2018 13:50:16 GMT
{
"id": "89f57064-b4b4-4e1a-adae-142d8c09d834",
"createdDate": "2018-10-27T13:50:16.803+0000",
"createdBy": "testsystem",
"lastModifiedDate": "2018-10-27T13:50:16.803+0000",
"lastModifiedBy": "testsystem",
"street": "exStreet",
"houseNumber": "121",
"zipCode": "14321",
"city": "exCity",
"state": "exState",
"_links": {
"self": {
"href": "http://localhost:8080/addresses/89f57064-b4b4-4e1a-adae-142d8c09d834"
},
"address": {
"href": "http://localhost:8080/addresses/89f57064-b4b4-4e1a-adae-142d8c09d834"
},
"person": {
"href": "http://localhost:8080/addresses/89f57064-b4b4-4e1a-adae-142d8c09d834/person"
}
}
}
Response code: 201; Time: 225ms; Content length: 693 bytes由此产生的人:
{
"id" : "89f57064-b4b4-4e1a-adae-142d8c09d834",
"createdDate" : "2018-10-27T13:50:16.808+0000",
"createdBy" : "testsystem",
"lastModifiedDate" : "2018-10-27T13:50:16.808+0000",
"lastModifiedBy" : "testsystem",
"lastName" : "ExampleLastName",
"firstName" : "ExampleFirstName",
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/89f57064-b4b4-4e1a-adae-142d8c09d834"
},
"person" : {
"href" : "http://localhost:8080/persons/89f57064-b4b4-4e1a-adae-142d8c09d834"
}
}
}数据库:
SELECT * FROM ADDRESS;
CREATED_BY CREATED_DATE LAST_MODIFIED_BY LAST_MODIFIED_DATE CITY HOUSE_NUMBER STATE STREET ZIP_CODE PERSON_ID
testsystem 2018-10-27 15:50:16.803 testsystem 2018-10-27 15:50:16.803 exCity 121 exState exStreet 14321 89f57064b4b44e1aadae142d8c09d834
SELECT * FROM PERSON;
ID CREATED_BY CREATED_DATE LAST_MODIFIED_BY LAST_MODIFIED_DATE FIRST_NAME LAST_NAME
89f57064b4b44e1aadae142d8c09d834 testsystem 2018-10-27 15:50:16.808 testsystem 2018-10-27 15:50:16.808 ExampleFirstName ExampleLastName就是这样。感谢您的阅读。
https://stackoverflow.com/questions/53017471
复制相似问题