0

i have the following hibernate mapping:

Class Magazine:

@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;

Class 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;

Class 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());
}

I tried alot of stuff, but somehow i could not manage to find a good way to do deletes on the mappings. If i delete a keyword i want all the mappings containing this keyword to be deleted. Somehow all i get are IntegrityConstraintViolations...what am i doing wrong? I use DAO's and call EntityManager.remove() on the keyword object. I also tried manually removing the mappings from the associated Magazine objects. Any hints are highly appreciated. Thanks for your help guys!:)

user871784
  • 1,247
  • 4
  • 13
  • 32

3 Answers3

0

as I see that you not use lazy fetch type. It will solve your problem. Use a similar solution:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "xxx")
mig8
  • 122
  • 4
0

Cascade strategy is what you need to look

Hibernate Documentation

Stackoverflow thread

Community
  • 1
  • 1
Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47
0

Found the problem: i had a constraint on the DB that restricted deletes. Set it to cascade, now everything is working perfectly!

user871784
  • 1,247
  • 4
  • 13
  • 32