这里的问题是,为什么在调用实体的一些setter后不保存实体属性。通常,在更改托管实体的属性时,它应该传播到数据库。
看看这个例子:
@Service
public class SystemServiceImpl implements SystemService {
@Autowired
private SystemDao systemDao;
@Override
@Transactional
public System replace(Long systemID) {
// External system to replace
System system = systemDao.findByID(systemID);
if (null != system) {
system.setName("Test"); // Calling findByID again shows that this call did not have any effect.
}
return system;
}
}
-
@Entity
@Table(name = "db.system")
public class System {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long systemID;
private String name;
@OneToMany(mappedBy = "system", fetch = FetchType.LAZY)
@JsonIgnore
private List<Customer> customers = new ArrayList<Customer>();
public Long getSystemID() {
return systemID;
}
public void setSystemID(Long systemID) {
this.systemID = systemID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Customer> getCustomers() {
return customers;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
}
如果我叫systemDao。在系统后合并。然后将其保存到数据库。我觉得我不应该调用merge,因为它应该是一个托管实体。
我试着用带有@Transactional和不带@Transactional的replace方法,两者都产生了相同的结果。
有什么想法吗?