Spring hibernate不会持久化对象节点

时间:2017-03-21 20:01:53

标签: java spring hibernate jpa

我有以下实体:

@Entity
public class Pages {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name="page")
private List<Pages_lang> pages_lang;

private long updated;
private Boolean deleted = false;
private long position;
private String icon;

@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "parent")
private List<Pages> children;

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Pages parent;

这一个:

@Entity
public class Pages_lang {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "language")
    private Language language;

    private String title;

    @Column(name = "text", columnDefinition = "TEXT")
    private String text;

    @Column(name = "plaintext", columnDefinition = "TEXT")
    private String plaintext;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)//, mappedBy = "articles_lang")
    @JoinColumn(name = "pages_lang")
    private List<Images> images;

当我在DB whit这个方法中持续(创建)时,它没有任何问题:

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> postPageTopLevel(@RequestBody Pages posted) {
Pages inserted = new Pages();
inserted.merge(posted);

Pages result = dao.save(inserted);

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(ServletUriComponentsBuilder
        .fromCurrentRequest().path("/" + result.getId())
        .buildAndExpand(result.getId()).toUri());

return new ResponseEntity<String>(getGson().toJson(result), httpHeaders, HttpStatus.CREATED);
}

但是当我想更新Pages_lang对象的属性并且我持久保存Page对象时,该属性不会在数据库上修改。

这里,更新方法:

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity<String> putPage(@PathVariable Long id, @RequestBody Pages puted) {
    System.out.println("");System.out.println("");System.out.println("");System.out.println("");System.out.println("");System.out.println("");

    if (id != puted.getId())
        return new ResponseEntity<String>("URI unmatch element", new HttpHeaders(), HttpStatus.FORBIDDEN);

    Pages page = dao.findOne(id);
    if (page == null)
        return new ResponseEntity<String>(null, new HttpHeaders(), HttpStatus.NOT_FOUND);

    dao.save(puted);

    return new ResponseEntity<String>(getGson().toJson(puted), new HttpHeaders(), HttpStatus.OK);
}

我认为该问题与Pages对象中的Pages_lang属性的Cascade类型有关。

任何人都可以帮助我?

问候

编辑:

dao.save()方法是Spring Repository中包含的基本方法。

BaseRepository.java

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {

void delete(T deleted);

Iterable<T> findAll();

T findOne(ID id);

T save(T persisted);

Iterable<T> save(Iterable<T> persited);
}

0 个答案:

没有答案