从@ElementCollection获取后将删除行

时间:2014-08-26 21:36:30

标签: hibernate postgresql jpa spring-data

我创建了一个名为Media的Entity,其中包含与Media相关的标记列表。我使用@ElementCollection来保存标签。我根据这篇文章对它进行了注释问题是当我在阅读该字段时,我第一次获得内容,但是它被删除了。

我创建了一个存储库:

mrepo的:

public interface MRepo extends CrudRepository<Media, String> {}

当我使用该服务获取媒体时,删除发生了:

...    
@Autowired
    MediasRepository mediasRepository;

    public MediaReadEvent findMedia(String mediaID){
    Media media = mediasRepository.findOne(mediaID);
...

Hibernate SQL Log:

hibernate: select tags0_.media_name as media_na1_2_0_, tags0_.tag_name as tag_name2_12_0_, tags0_.tags_ORDER as tags_ORD3_0_ from media_tags tags0_ where tags0_.media_nam
?
hibernate: delete from media_tags where media_name=?
hibernate: select tags0_.media_name as media_na1_2_0_, tags0_.tag_name as tag_name2_12_0_, tags0_.tags_ORDER as tags_ORD3_0_ from media_tags tags0_ where tags0_.media_nam
?
hibernate: delete from media_tags where media_name=?
hibernate: select tags0_.media_name as media_na1_2_0_, tags0_.tag_name as tag_name2_12_0_, tags0_.tags_ORDER as tags_ORD3_0_ from media_tags tags0_ where tags0_.media_nam
?
hibernate: delete from media_tags where media_name=?

媒体实体:

@Entity
public class Media {
  @Id
  @NotNull
  @Column(name = "mediaid")
  private String mediaId;
  ...
//    @NotNull
    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(
            name="media_tags",
            joinColumns=@JoinColumn(name="media_name")
    )
    @Column(name="tag_name")
    @OrderColumn
    private List<String> tags;


  public List<String> getTags() {
    return tags;
  }

  public void setTags(List<String> tags) {
    this.tags = tags;
  }
  ...
}

万一重要,我正在使用: postgresql - 9.2-1003-jdbc4 spring-data-jpa - 1.5.1.RELEASE 春天 - 3.2.8.RELEASE

1 个答案:

答案 0 :(得分:0)

任何解决方案?我似乎遇到了同样的问题。

也许this question and answer is helpful

我发现在更新到版本4.2.7(Redhat)到版本4.3.1.Final-redhat-1之后,我遇到的同样问题似乎已经消失。我还使用了ENABLE_LAZY_LOAD_NO_TRANS功能。