-->
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.  [ 2 posts ] 
Author Message
 Post subject: Delete +Update not working in OnetoMany Unidrectional
PostPosted: Mon Aug 02, 2010 8:17 am 
Newbie

Joined: Mon Aug 02, 2010 7:56 am
Posts: 2
This is my first post. I am trying s sample where I have onetomany relationship

Code:
CREATE TABLE stock (
    STOCK_ID     int(10) AUTO_INCREMENT NOT NULL,
    STOCK_CODE   varchar(10) NOT NULL,
    STOCK_NAME   varchar(20) NOT NULL,
    PRIMARY KEY(STOCK_ID)
)
CREATE TABLE stock_daily_record (
    RECORD_ID      int(10) AUTO_INCREMENT NOT NULL,
    PRICE_OPEN     float NULL,
    PRICE_CLOSE    float NULL,
    PRICE_CHANGE   float NULL,
    VOLUME         bigint(20) NULL,
    DATE           date NOT NULL,
    STOCK_ID       int(10) NOT NULL,
    PRIMARY KEY(RECORD_ID),
   FOREIGN KEY(STOCK_ID) REFERENCES STOCK(STOCK_ID)

)

//Stock.java
   .....
   @OneToMany(fetch = FetchType.EAGER, mappedBy = "stock",cascade=CascadeType.ALL)    
   @JoinColumn(name = "STOCK_ID")
       @Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
   public Set<StockDailyRecord> getStockDailyRecords() {
      return stockDailyRecords;
   }

//StockDailyRecord.java
.....
      @ManyToOne(fetch = FetchType.EAGER)
       @JoinColumn(name="STOCK_ID")   
   public Stock getStock() {
      return this.stock;
       }


//StockDAOTest.java
//I am trying to first delete 2 records in Stock_daily_reocord associated with a stock[CHILD DELETE] and then associate a new stock daily reocord with that stock[UPDATE]
@Test
   public void update() {
      Set<StockDailyRecord> records = new HashSet<StockDailyRecord>(0);
      Stock stock = new Stock();
      Integer stockId = new Integer(59);
      int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
      Date currentDate = new Date();
      int year = currentDate.getYear();
      int month = currentDate.getMonth();
      int date = currentDate.getDay();

      GregorianCalendar oneWeek = new GregorianCalendar(year, month, date);
      oneWeek.add(GregorianCalendar.DATE, 8);
      Date date2 = oneWeek.getTime();

      stock.setStockId(stockId);

      Stock foundStock = (Stock) stockDao.findById(stockId);
      System.out.println("After found stock is " + foundStock);
      
      
      Set<StockDailyRecord> foundRecords = foundStock.getStockDailyRecords();   
      Iterator iter = foundRecords.iterator();
      
      
      
     for (Iterator i = foundRecords.iterator(); i.hasNext(); ){
          StockDailyRecord foundRecord = (StockDailyRecord) i.next();
          System.out.println("Deleting foundRecord= " + foundRecord);      
          stockDailyRecordDAO.delete(foundRecord);
          System.out.println("Deleted foundRecord= " + foundRecord);
       }
      
      
    

   
        //Update Stock
        foundStock.setStockCode("1111");
      System.out.println("After deleting StockDailyRecords found stock is " );
      System.out.println( foundStock);
      System.out.println("After deleting StockDailyRecords size is " );
      System.out.println( foundRecords.size());
      StockDailyRecord stockDailyRecord = new StockDailyRecord();
      stockDailyRecord.setPriceOpen(new Float("7.2"));
      stockDailyRecord.setPriceClose(new Float("7.2"));
      stockDailyRecord.setPriceChange(new Float("7.2"));
      stockDailyRecord.setVolume(2000000L);
      stockDailyRecord.setDate(date2);
      stockDailyRecord.setStock(foundStock);
      //foundRecords = foundStock.getStockDailyRecords();   
      foundRecords.add(stockDailyRecord);
      System.out.println("After updating StockDailyRecords size is " );
      System.out.println( foundRecords.size());
      
      //stockDailyRecordDAO.save(stockDailyRecord);
      System.out.println("Updating stock with stockDailyRecord==" + stockDailyRecord);
      foundStock.setStockDailyRecords(foundRecords);
      System.out.println("Updating stock==" + foundStock);
      stockDao.update(foundStock);

   }
   }


Issue:
The code does not semm to be deleting child records. I see that it dies inside for loop and it does not even iterate after first record. All I see is the message" Closing JPA EntityManagerFactory for persistence unit " after first iteration.Here is the log:
Code:
05:14:49,192  INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured
05:14:49,442  INFO StockDAO:161 - finding Stock instance with id: 59
05:14:49,520  INFO StockDAO:164 - found Stock instance with id: 59
After found stock is Stock [stockCode=1111, stockId=59, stockName=HENM]
Deleting foundRecord= StockDailyRecord [date=0110-08-09, priceChange=6.2, priceClose=6.2, priceOpen=6.2, recordId=6, stock=Stock [stockCode=1111, stockId=59, stockName=HENM], volume=2000000]
05:14:49,551  INFO StockDailyRecordDAO:107 - deleting StockDailyRecord instance StockDailyRecord [date=0110-08-09, priceChange=6.2, priceClose=6.2, priceOpen=6.2, recordId=6, stock=Stock [stockCode=1111, stockId=59, stockName=HENM], volume=2000000]
05:14:49,567  INFO StockDailyRecordDAO:112 - delete successfulStockDailyRecord [date=0110-08-09, priceChange=6.2, priceClose=6.2, priceOpen=6.2, recordId=6, stock=Stock [stockCode=1111, stockId=59, stockName=HENM], volume=2000000]
05:14:49,598  INFO GenericApplicationContext:978 - Closing org.springframework.context.support.GenericApplicationContext@1d332b: startup date [Mon Aug 02 05:14:47 PDT 2010]; root of context hierarchy
05:14:49,598  INFO DefaultListableBeanFactory:421 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e234c: defining beans [entityManagerFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,StockDailyRecordDAO,StockDAO,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
05:14:49,598  INFO LocalEntityManagerFactoryBean:406 - Closing JPA EntityManagerFactory for persistence unit 'HibernateAnnotationExamplePU'
05:14:49,598  INFO SessionFactoryImpl:853 - closing


Top
 Profile  
 
 Post subject: Re: Delete +Update not working in OnetoMany Unidrectional
PostPosted: Mon Aug 02, 2010 8:27 am 
Newbie

Joined: Mon Aug 02, 2010 7:56 am
Posts: 2
Ok 2 things:
1)Although ,the above code is oneto many birectional but I have tried with onetomany unidrectinal as well .
2)The for loop for delete which is wrongly posted is actually:
Code:
for (Iterator i = foundRecords.iterator(); i.hasNext(); ){
          StockDailyRecord foundRecord = (StockDailyRecord) i.next();
          System.out.println("Deleting foundRecord= " + foundRecord);
          foundStock.getStockDailyRecords().remove(foundRecord);
          stockDailyRecordDAO.delete(foundRecord);
          System.out.println("Deleted foundRecord= " + foundRecord);
       }


Still having same issue.Experts..please help.


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

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.