I가 다음 엔터티Hibernate는 객체의 모든 필드를 유지하지 않습니다
@Entity
@Table(name = "Example")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Example implements Comparable<Example>, Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id;
String fieldToPersist;
}
그것의 DAO 내가 들어
example.setFieldToPersist("abc")
dao.createOrsave(example);
을 설정
내 코드에서
public class ExampleDAO {
public SessionFactory sessionFactory;
public Session session;
public ExampleDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
this.session = sessionFactory.openSession();
}
@Transactional
public void createOrSave(Example ex) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Example exExisting = getById(ex.id);
try {
if (exExisting != null) {
session.merge(ex);
tx.commit();
} else {
session.save(ex);
}
} catch (RuntimeException e) {
tx.rollback();
} finally {
session.close();
}
}
이것이 다에서 지속되지 않는 몇 가지 이유 tabase. DAO 방식으로 호출되는 것을 볼 수 있으며 로그에 오류가 표시되지 않습니다. 그러나이 필드는 데이터베이스에 저장되지 않습니다. (개체가 저장 되더라도)
session.merge()
호출에 문제가 있다고 생각됩니다. 개체를 저장하는 경우에만 제거하면 개체의 새 행이 만들어 지지만 새 필드가 저장됩니다. 제가 누락 된 것이 있습니까?
내가 처음으로 객체를 변경하고 createOrSave() 메소드를 호출 할 때 객체를 올바르게 업데이트한다는 사실을 확인했습니다. 그러나이 메소드에 대한 향후 호출은 그것을 업데이트하는 것처럼 보이지 않는다 ?? 세션이 오래 가지 않습니까? 로그가 이것에 대한 정보를 제공해야합니까?
또한 merge를 호출하기 바로 전에 필드의 값을 확인했으며 새로운 값입니다. 이것이 왜 데이터베이스에 반영되지 않은 것입니까?
는 또한session.save() -- creates a new entry in the database with the updated values
session.update() - no change
session.saveOrUpdate() -- no change
session.flush() -- no change
덕분에, 그것을 변경하지만
그 나던 문제를 해결하는 것 –또한 전화를 걸어 볼 수도 있습니다 '예 ex = getById (ex.id); '후 : session.beginTransaction() = TX '거래,' – KuroObi
같은 문제를 trabsaction 후 개체가 게터에 설정해야 주석이 있습니까 –