子エンティティ (エージェンス) の 1 つを変更してエンティティ (クライアント) を更新しようとしていますが、リポジトリの保存方法は作成方法と同じ動作をしません。
新しいエージェンス 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
作成クライアント:OK
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** !!!!