Client
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "client")
public class Client implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "cpf", nullable = false)
private String cpf;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "registration_data")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM-dd-yyyy HH:mm", timezone = "GMT") //ISO 8601
private Date registrationData;
@OneToMany(mappedBy = "clientId")
private List<Contact> contacts;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "clientId")
@JoinColumn(name = "address")
private Address address;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "pets")
private List<Pet> pets;
@Column(name = "type_profile", nullable = false)
private String typeProfile;
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "image")
private byte[] image;
}
地址
@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "address")
public class Address implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "clientId")
private Client clientId;
@Column(name = "publicPlace")
private String publicPlace;
@Column(name = "city")
private String city;
@Column(name = "district")
private String district;
@Column(name = "complement")
private String complement;
@Column(name = "tag")
private String tag;
}
我试着扰乱一对多的关系,但它没有工作。
没有一个关系按预期工作,保存了id。
在数据库中只保存第一个对象,而不需要在相关表中添加ID。
我不知道我可能做错了什么,因为它保存了对象并在表中创建了ID,但它没有在第二个对象中作为级联进行关联。
1条答案
按热度按时间pgx2nnw81#
您是否应该更改为以下代码,并使用client.getId()来使用客户端ID?您必须确保数据库中名为client的表存在