2014-03-18 124 views
0

我有以下hibernate映射:休眠刪除關聯表條目時關聯的一個部分被刪除

類雜誌:

@Entity 
@Table(name = "PROJECTNAME_MAGAZINE") 
@SequenceGenerator(name = "MAGAZINE_SEQUENCE_GENERATOR", sequenceName = "MAGAZINE_SEQUENCE") 
@Analyzer(definition = "customanalyzer") 
public class Magazine extends AbstractBaseEntity implements Serializable{ 


@Id 
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MAGAZINE_SEQUENCE_GENERATOR") 
@Column(name = "ID") 
private Long id; 
@NotNull 
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO) 

@LazyCollection(LazyCollectionOption.FALSE) 
@OneToMany(cascade = CascadeType.ALL, mappedBy="magazine", orphanRemoval = true) 
private List<MagazineKeywordMapper> magazineKeywordMappings; 

類MagazineKeyword

@Entity 
@Table(name = "PROJECTNAME_MAGAZINE_KEYWORD") 
public class MagazineKeyword extends AbstractBaseEntity { 

public MagazineKeyword(String keyword) { 
    this.keyword = keyword; 
    this.id = (long) keyword.hashCode(); 
} 

@Id 
@Column(name = "ID") 
private Long id; 

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="keyword",orphanRemoval=true) 
private List<MagazineKeywordMapper> magazineMappings; 

類MagazineKeywordMapper

@Entity 
@Table(name="MAGAZINE_KEYWORD_MAPPER") 
public class MagazineKeywordMapper extends AbstractBaseEntity implements Serializable { 

private static final long serialVersionUID = 1L; 

@Id 
@Column(name = "ID") 
private Long id; 

@ManyToOne 
    @PrimaryKeyJoinColumn(name="MAGAZINE_ID", referencedColumnName="ID") 
    private Magazine magazine; 


    @ManyToOne 
    @PrimaryKeyJoinColumn(name="KEYWORD_ID", referencedColumnName="ID") 
    private MagazineKeyword keyword; 

    public MagazineKeywordMapper() { 

    } 

public MagazineKeywordMapper(Magazine magazine, MagazineKeyword keyword) { 
    this.magazine = magazine; 
    this.keyword = keyword; 
    this.id = Long.parseLong(magazine.getId()+""+keyword.getId()); 
} 

我嘗試了很多東西,但不知何故,我無法找到一種好辦法在映射上進行刪除。如果我刪除了一個關鍵字,我希望所有包含此關鍵字的映射都被刪除。不知何故,我得到的是IntegrityConstraintViolations ...我做錯了什麼?我使用DAO並在關鍵字對象上調用EntityManager.remove()。我也嘗試手動刪除相關雜誌對象的映射。任何提示都非常感謝。感謝您的幫助球員!:)

回答

0

發現什麼問題:我有上限制刪除數據庫約束。將它設置爲級聯,現在一切都完美了!

0

因爲我看到你不使用懶惰的獲取類型。它會解決你的問題。使用類似的解決方案:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "xxx") 
+0

謝謝我會試試... – user871784

+0

FetchType.Lazy不幸的是不會改變任何東西:// – user871784