Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPA Bidirectional OneToMany with Hibernate DELETE_ORPHAN not working ok !?

843833Mar 4 2008 — edited Mar 27 2008
Hello there,

I'm using a bidirectional OneToMany relation and I've annotated the invers side of the relation with Hibernate extension cascade DELETE_ORPHAN and the JPA cascade MERGE and PERSIST.
NOTE: on the entity that owns the relation, I have an unique constraint on some fields.

Now here is the problem: When I remove an entity from the collection of the inverse side and I add another entity with the same columns that forms the unique constraint, the merge operation crashes because first is made the insert and only after that the delete occurs.

For a better immage here is a snap from the code:
@Entity
public class BudgetIncome implements Serializable {
......
   @OneToMany(targetEntity = AccountIncomeMapping.class,
      cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "budgetIncome")
  @JoinColumns({@JoinColumn(name = "ID_VERSIUNE", referencedColumnName = "ID_VERSIUNE"),
                @JoinColumn(name = "ID_SURSA_VENIT", referencedColumnName = "ID_SURSA_VENIT")})
  @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  List<AccountIncomeMapping> accountMappings;
.....
}
@Entity
@Table(name = "T_MAPARE_CONTURI_VENITURI",
    uniqueConstraints = @UniqueConstraint(columnNames = {"ID_CONT", "ID_VERSIUNE", "ID_SURSA_VENIT"}))
public class AccountIncomeMapping implements Serializable {
   
   @Id
  @Column(name = "ID")
  @SequenceGenerator(name = "accountIncomeMappingGenerator", sequenceName = "T_MAPARE_CONTURI_VENITURI_SEQ")
  @GeneratedValue(generator = "accountIncomeMappingGenerator")
  private Integer Id;

  @ManyToOne(optional = false)
  @JoinColumn(name = "ID_CONT", referencedColumnName = "ID", updatable = false)
  private Account account;

  @ManyToOne(optional = false)
  @JoinColumn(name = "ID_VERSIUNE", referencedColumnName = "ID_VERSIUNE", updatable = false)
  private com.iquestint.budgeting.logic.model.bsc.Version version;

  @ManyToOne(optional = false)
  @JoinColumn(name = "ID_SURSA_VENIT", referencedColumnName = "ID_SURSA_VENIT", updatable = false)
  private IncomeSource incomeSource;

  @ManyToOne
  @JoinColumns({@JoinColumn(name = "ID_VERSIUNE", referencedColumnName = "ID_VERSIUNE", updatable = false, insertable = false), @JoinColumn(
      name = "ID_SURSA_VENIT", referencedColumnName = "ID_SURSA_VENIT", updatable = false, insertable = false)})
  private BudgetIncome budgetIncome;
...
}
Now consider that I have the following mappings:
*BudgetIncome {*
AccountIncomeMapping -> ID = 1, ID_CONT = 2, ID_VERSIUNE = 2, ID_SURSA_VENIT = 2 ...
AccountIncomeMapping -> ID = 2, ID_CONT = 3, ID_VERSIUNE = 3, ID_SURSA_VENIT = 3 ...
*}*

and I try to remove the first AccountIncomeMapping (with ID=1) and add a new AccountIncomeMapping (ID will be null) but with the same fields that forms the unique constraint:
*BudgetIncome {*
AccountIncomeMapping -> ID = null, ID_CONT = 2, ID_VERSIUNE = 2, ID_SURSA_VENIT = 2 ...
AccountIncomeMapping -> ID = 2, ID_CONT = 3, ID_VERSIUNE = 3, ID_SURSA_VENIT = 3 ...
*}*

What happens is that the insert will be done first and will crash with unique constraint violation. However, if I remove the unique constraint, the remove will take place but only after the insert is done.

The question is:
Is there any way to inform the persistence provider that the insert should take place only after the delete?


Thanks a lot

Edited by: vali_ on Mar 4, 2008 12:22 AM for being easier to follow the code
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 24 2008
Added on Mar 4 2008
2 comments
882 views