-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Common target of @OnetoMany relation
PostPosted: Mon Sep 19, 2011 4:58 am 
Newbie

Joined: Mon Jul 18, 2011 11:23 am
Posts: 4
Hi,

Hibernate version - 3.6.1.FINAL
DB - MySQL

I have the following relationship among entities -


Amount has one or more TaxAmount
Tax has one or more TaxAmount

,there is no relation ship b/w Amount and Tax.

The entities are are as follows -

1) Amount
Code:

package com.uti.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.envers.Audited;

@Entity @Audited @Table(schema="mysql")
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Cacheable @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Amount implements Serializable{
   @Id @GeneratedValue
   private long amountId;
   public long getAmountId() {
      return amountId;
   }
   public void setAmountId(long amountId) {
      this.amountId = amountId;
   }
   public String getAmountCode() {
      return amountCode;
   }
   public void setAmountCode(String amountCode) {
      this.amountCode = amountCode;
   }
   public List<TaxAmount> getTaxAmountsAmt() {
      if (taxAmountsAmt == null) {
         taxAmountsAmt = new ArrayList<TaxAmount>();
      }
      this.taxAmountsAmt.removeAll(Collections.singleton(null));
      return this.taxAmountsAmt;

   }
   public void setTaxAmountsAmt(List<TaxAmount> taxAmounts) {
      if (taxAmounts == null) {
            this.taxAmountsAmt = new ArrayList<TaxAmount>();
        } else {
           this.taxAmountsAmt = taxAmounts;
        }
      
   }
   private String amountCode;
   @OneToMany (cascade=CascadeType.ALL,orphanRemoval=true)
    @JoinColumn(name="amount_id", insertable=false, updatable=false, nullable=false)
   @IndexColumn(name="indx",base=0)
   protected List<TaxAmount> taxAmountsAmt = new ArrayList<TaxAmount>();

}




2) Tax

Code:

package com.uti.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.envers.Audited;
@Entity @Audited @Table(schema="mysql")
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Cacheable @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Tax implements Serializable {
   @Id @GeneratedValue
   private long taxId;
   public long getTaxId() {
      return taxId;
   }
   public void setTaxId(long taxId) {
      this.taxId = taxId;
   }
   public String getTaxCode() {
      return taxCode;
   }
   public void setTaxCode(String taxCode) {
      this.taxCode = taxCode;
   }
   private String taxCode;
   @OneToMany (cascade=CascadeType.ALL,orphanRemoval=true)
    @JoinColumn(name="tax_id", insertable=false, updatable=false, nullable=false)
   @IndexColumn(name="indx",base=0)
   protected List<TaxAmount> taxAmountsTax = new ArrayList<TaxAmount>();
   public List<TaxAmount> getTaxAmountsTax() {
      if (taxAmountsTax == null) {
         taxAmountsTax = new ArrayList<TaxAmount>();
      }
      this.taxAmountsTax.removeAll(Collections.singleton(null));
      return this.taxAmountsTax;

   }
   public void setTaxAmountsTax(List<TaxAmount> taxAmounts) {
      if (taxAmountsTax == null) {
            this.taxAmountsTax = new ArrayList<TaxAmount>();
        } else {
           this.taxAmountsTax = taxAmounts;
        }
      
   }

}




3) TaxAmount
Code:

package com.uti.entity;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.envers.Audited;


@Entity @Audited @Table(schema="mysql")
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Cacheable @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class TaxAmount implements Serializable {
   @Id @GeneratedValue
   private long taxAmountId;
   public long getTaxAmountId() {
      return taxAmountId;
   }
   public void setTaxAmountId(long taxAmountId) {
      this.taxAmountId = taxAmountId;
   }
   public BigDecimal getTaxAmount() {
      return taxAmount;
   }
   public void setTaxAmount(BigDecimal taxAmount) {
      this.taxAmount = taxAmount;
   }
   public Tax getTaxProxy() {
      return taxProxy;
   }
   public void setTaxProxy(Tax taxProxy) {
      this.taxProxy = taxProxy;
   }
   public Amount getAmountProxy() {
      return amountProxy;
   }
   public void setAmountProxy(Amount amountProxy) {
      this.amountProxy = amountProxy;
   }
   private BigDecimal taxAmount;
   @ManyToOne
   @JoinColumn(name="tax_id",insertable=false, updatable=false, nullable=false)
   private Tax taxProxy;
   @ManyToOne
   @JoinColumn(name="amount_id",insertable=false, updatable=false, nullable=false)
   private Amount amountProxy;
   
   /*@Column(name="indx",insertable=false,updatable=false)
   private int indx;
   public int getIndx() {
      return indx;
   }
   public void setIndx(int indx) {
      this.indx = indx;
   }
   */
   
   

   
   
   

}




Code used to insert data -

Code:
em.getTransaction().begin();

Amount amt = new Amount();
amt.setAmountCode("SAL");

em.persist(amt);

Tax tax = new Tax();
tax.setTaxCode("TDS");

em.persist(tax);


TaxAmount taxAmount = new TaxAmount();
taxAmount.setTaxAmount(new BigDecimal(100));

amt.getTaxAmountsAmt().add(taxAmount);
taxAmount.setAmountProxy(amt);
tax.getTaxAmountsTax().add(taxAmount);
taxAmount.setTaxProxy(tax);

em.getTransaction().commit();
         




The above code errors out with following exception -

Code:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.uti.entity.TaxAmount column: indx (should be mapped with insert="false" update="false")
   at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
   at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
   at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
   at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
   at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1332)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1835)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)



This works if I use different names for @IndexColumn annotation.But if I use 2 different names, 2 separate index columns are created in DB.

Is it possible to make it work with only one index column?


Also, in case I need to add a new TaxAmount under an existing tax and Amount, please advice how would I do it?

Regards,
Jacob


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.