私のエンティティオブジェクトをフェッチしていない:私はSystem.outにコメントを解除した場合FetchType.EAGERが
@Entity
@NoArgsConstructor
public class Company {
@Id
@Getter
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "agent_id")
private Agent agent;
}
と
@Entity
@NoArgsConstructor
public class Agent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
private Long id;
@Column(unique=true)
@Getter
private String name;
}
と問題の方法を
@Transactional
public void update(Company entity) {
Company existing = companyRepository.getOne(entity.getId());
//System.out.println(existing.getAgent().getName());
em.detach(existing);
System.out.println(existing.getAgent()); // => org.hibernate.LazyInitializationException: could not initialize proxy - no Session
}
最終ラインはLazyInitializationExceptionは、原因となります。 println()、すべて正常に動作します。だからFetchType.LAZYのように見えます。私は間違って何をしていますか?
ではありません。 xml <プロパティ名= "hibernate.enable_la zy_load_no_trans "> true –