我正在尝试通过修改其子实体(代理)之一来更新实体(客户端),但存储库保存方法的行为与创建方法不同。
当我通过提供新的代理 ID 更新客户端时,代理未加载。
有人能解释一下为什么以及如何使这成为可能吗?
实体客户端:
@Entity
@Table(name = "client")
public class ClientEntity extends Serializable{
private static final long serialVersionUID = -7451447955767820762L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int reference;
@ManyToOne
@JoinColumn(name = "agence_id")
private AgenceEntity agence;
// getter/setter
}
实体机构:
@Entity
@Table(name = "agence")
public class AgenceEntity extends AbstractGenericEntity {
private static final long serialVersionUID = -3674920581185152947L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int id;
@Column(nullable = false, length = 100)
private String name;
// getter/setter
}
表代理:
Id | Name
1 | name1
2 | name2
表客户端:
Reference | agence_id
1 | 1
创建客户端:好的
Agence agence = new Agence();
agence.setId(1); // load agence #1
Client client = new Client();
client.setAgence(agence);
client = clientRepository.save(client);
System.out.println(client.getReference); // print '2'
System.out.println(client.getAgence().getName()); // print 'name1'
更新客户端:NOK
Client client = clientRepository.findOne(1); // load client #1
Agence agence = new Agence();
agence.setId(2);
client.setAgence(agence); // update agence with agence #2
client = clientRepository.save(client);
System.out.println(client.getReference); // print '1'
System.out.println(client.getAgence().getName()); // print **NULL** !!!!