-->
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.  [ 13 posts ] 
Author Message
 Post subject: org.hibernate.session.save doesn't save in database
PostPosted: Wed Dec 13, 2006 12:24 pm 
Newbie

Joined: Wed Dec 13, 2006 12:15 pm
Posts: 6
Hi,
I'm having a strange problem with hibernate:

When trying to pressist an object by using:

sess.save(my_object);
(sess is a hibernate Session)

It seems as if everything is going just fine, I get no exception and I can print my_object's fields in the java code (System.out.println(my_object.getSomeField);) but when checking the appropriate row for this object in postgresql, it seems as if the field (of which the right value was printed) is empty!

What might be the reason for this strange behavior?

Thanks in advance,
Idoido.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 12:52 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
http://www.hibernate.org/160.html

No mappings + no code = no way to help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 2:09 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Ananasi wrote:
http://www.hibernate.org/160.html

No mappings + no code = no way to help

I completely agree.

Trying to be a medium on what your problem is, though, I guess you didn't start and commit a transaction. You must act this way :

Code:
session.beginTransaction();
session.save(yourObject);
session.getTransaction().commit();


Remember that every dialogs with DB should never be tried outside a tx context, yes even a select clause should be started by a "begin" and commited at the end.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 2:41 pm 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
Can you please supply us with the log file + persistant class+ mapping file+ code sample where you tried to save

Thanks

_________________
Alan Mehio
London
UK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 5:58 pm 
Newbie

Joined: Fri Dec 09, 2005 4:57 am
Posts: 7
Hi,

Somehow, my problem is similar to the one described by the original poster of this thread. I am able to save a new bean to the database, however, I am not able to save updates to the database. I will supply as much detail as I can below in order to get a better handle on the problem.

Any idea? I have been struggling with this one for quite a few days.

thanks.

Rauf


1. Snipped from the log file:

2006-12-13 03:50:15,836 DEBUG [com.hunza.kcw.db.UserManager] - Updating user
2006-12-13 03:50:15,836 DEBUG [org.hibernate.transaction.JDBCTransaction] - begin
2006-12-13 03:50:15,836 DEBUG [org.hibernate.transaction.JDBCTransaction] - current autocommit status: false
2006-12-13 03:50:15,856 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - ignoring persistent instance
2006-12-13 03:50:15,856 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - object already associated with session: [com.hunza.kcw.bus.User#4]
2006-12-13 03:50:15,856 DEBUG [org.hibernate.transaction.JDBCTransaction] - commit
2006-12-13 03:50:15,856 DEBUG [org.hibernate.transaction.JDBCTransaction] - committed JDBC Connection
2006-12-13 03:50:15,856 DEBUG [com.hunza.kcw.Hibernate.HibernateUtil] - Closing Session of this thread.
2006-12-13 03:50:15,856 DEBUG [org.hibernate.impl.SessionImpl] - closing session
2006-12-13 03:50:15,856 DEBUG [org.hibernate.jdbc.ConnectionManager] - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2006-12-13 03:50:15,856 DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] - returning connection to pool, pool size: 1
2006-12-13 03:50:15,856 DEBUG [com.hunza.kcw.db.UserManager] - User updated successfully
2006-12-13 03:50:15,856 DEBUG [com.hunza.kcw.db.UserManager] - User ID: 4


2. java code:

try {
logger.debug("Updating user");

Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.update(user);
tx.commit();
HibernateUtil.closeSession();
logger.debug("User updated successfully");
logger.debug("User ID: " + user.getUserId());
result = true;
} catch (HibernateException ex) {
logger.error("Error while updating the user: " + ex);
}



3. xml file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.hunza.kcw.bus.User" table="KCWUSERS">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="userId" column="USERID">
<generator class="increment"/>
</id>

<property name="userName">
<column name="USERNAME"/>
</property>

<property name="password">
<column name="PASSWORD"/>
</property>

<property name="email">
<column name="EMAIL"/>
</property>

<property name="lastLogOn">
<column name="LASTLOGON"/>
</property>



<property name="address1">
<column name="ADDRESS1"/>
</property>

<property name="address2">
<column name="ADDRESS2"/>
</property>

<property name="city">
<column name="CITY"/>
</property>

<property name="state">
<column name="STATE"/>
</property>

<property name="zip">
<column name="ZIP"/>
</property>

<property name="phone">
<column name="PHONE"/>
</property>


<property name="shippingAddress1">
<column name="SHIPPINGADDRESS1"/>
</property>

<property name="shippingAddress2">
<column name="SHIPPINGADDRESS2"/>
</property>

<property name="shippingCity">
<column name="SHIPPINGCITY"/>
</property>

<property name="shippingState">
<column name="SIHPPINGSTATE"/>
</property>

<property name="shippingZip">
<column name="SHIPPINGZIP"/>
</property>

<property name="shippingPhone">
<column name="SHIPPINGPHONE"/>
</property>


<property name="nameOnCard">
<column name="NAMEONCARD"/>
</property>

<property name="creditCardNumber">
<column name="CREDITCARDNUMBER"/>
</property>

<property name="expirationDate">
<column name="EXPIRATIONDATE"/>
</property>

<property name="cvv">
<column name="CVV"/>
</property>



<property name="comments">
<column name="COMMENTS"/>
</property>

<property name="sendUpdates">
<column name="SENDUPDATES"/>
</property>

<property name="firstName">
<column name="FIRSTNAME"/>
</property>

<property name="lastName">
<column name="LASTNAME"/>
</property>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: typo?
PostPosted: Wed Dec 13, 2006 6:55 pm 
Newbie

Joined: Wed Dec 06, 2006 6:30 pm
Posts: 8
bad post. ignore.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 6:12 am 
Newbie

Joined: Wed Dec 13, 2006 12:15 pm
Posts: 6
OK, here is the code that caused my problems:

Code:
try{
    Session sess = HibernateSessionFactory.getCurrentSession();
    <snip>.db.mappings.TOrder hOrder = new <snip>.db.mappings.TOrder();
    CopyBeanUtil.copy(order, hOrder);
    hOrder.setCustomer(loadHCustomer(customerid));
    hOrder.setVat(loadHVat(order.getVatid()));
    hOrder.setReseller(loadHReseller(order.getResellerid()));
    hOrder.setAccount(loadHAccount(order.getAccountid()));
    hOrder.setCampaignid(order.getCampaignid() );

    Long result = (Long) sess.save(hOrder);
         
    System.err.println("~~~~ "+hOrder.getOrderid()+" ~~~~");
    System.err.println("~~~~ "+hOrder.getCampaignid()+" ~~~~");

    return result;
} catch (Exception he) {
    he.printStackTrace();
}


the mapping:
Code:
package <snip>.db.mappings;
// Generated 19.07.2006 11:28:25 by Hibernate Tools 3.1.0.beta5

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class TOrder implements java.io.Serializable {
   // Fields   
   private Long orderid;
   private Date lastUpdated;
   private String status;
   private String txhandle;
   private Long parentorderid;
   private Long processinstanceid;
   private Long portalid;
   private Date billingdate;
   private BigDecimal billingamount;
   private BigDecimal netbillingamount;
   private Long paymentperiod;
   private Date actualcancellationdate;
   private Boolean ebanking;
   private BigDecimal dunningcosts;
   private Date dunningdate;
   private String customerip;
   private BigDecimal netsubtotal;
   private String remarks;
   private Set orderitems = new HashSet(0);
   private Vat vat;
   private Customer customer;
   private Reseller reseller;
   private Account account;
   private Invoices invoice;
        private String currency;
        private Long campaignid;
   
   // Constructors
   /** default constructor */
   public TOrder() {
   }
   /** minimal constructor */
   public TOrder(String status, Customer customer) {
      this.status = status;
      this.customer = customer;
   }
   /** full constructor */
   public TOrder(String status, String txhandle, Long parentorderid,         Long processinstanceid, Long portalid, Date billingdate, BigDecimal billingamount, BigDecimal netbillingamount, Long paymentperiod, Date actualcancellationdate, boolean banking,BigDecimal dunningcosts, Date dunningdate, String customerip,BigDecimal netsubtotal, String remarks, Set orderitems, Vat vat, Customer customer, Reseller reseller, Account account,Invoices invoice) {
      this.status = status;
      this.txhandle = txhandle;
      this.parentorderid = parentorderid;
      this.processinstanceid = processinstanceid;
      this.portalid = portalid;
      this.billingdate = billingdate;
      this.billingamount = billingamount;
      this.netbillingamount = netbillingamount;
      this.paymentperiod = paymentperiod;
      this.actualcancellationdate = actualcancellationdate;
      this.ebanking = ebanking;
      this.dunningcosts = dunningcosts;
      this.dunningdate = dunningdate;
      this.customerip = customerip;
      this.netsubtotal = netsubtotal;
      this.remarks = remarks;
      this.orderitems = orderitems;
      this.vat = vat;
      this.customer = customer;
      this.reseller = reseller;
      this.account = account;
      this.invoice = invoice;
   }

   public Long getOrderid() {
      return this.orderid;
   }
   public void setOrderid(Long orderid) {
      this.orderid = orderid;
   }
   public Date getLastUpdated() {
      return this.lastUpdated;
   }
   public void setLastUpdated(Date lastUpdated) {
      this.lastUpdated = lastUpdated;
   }
   public String getStatus() {
      return this.status;
   }
   public void setStatus(String status) {
      this.status = status;
   }
   public String getTxhandle() {
      return this.txhandle;
   }
   public void setTxhandle(String txhandle) {
      this.txhandle = txhandle;
   }
   public Long getParentorderid() {
      return this.parentorderid;
   }
   public void setParentorderid(Long parentorderid) {
      this.parentorderid = parentorderid;
   }
   public Long getProcessinstanceid() {
      return this.processinstanceid;
   }
   public void setProcessinstanceid(Long processinstanceid) {
      this.processinstanceid = processinstanceid;
   }
   public Long getPortalid() {
      return this.portalid;
   }
   public void setPortalid(Long portalid) {
      this.portalid = portalid;
   }

   public Date getBillingdate() {
      return this.billingdate;
   }

   public void setBillingdate(Date billingdate) {
      this.billingdate = billingdate;
   }

   public BigDecimal getBillingamount() {
      return this.billingamount;
   }

   public void setBillingamount(BigDecimal billingamount) {
      this.billingamount = billingamount;
   }

   public BigDecimal getNetbillingamount() {
      return this.netbillingamount;
   }

   public void setNetbillingamount(BigDecimal netbillingamount) {
      this.netbillingamount = netbillingamount;
   }

   public Long getPaymentperiod() {
      return this.paymentperiod;
   }

   public void setPaymentperiod(Long paymentperiod) {
      this.paymentperiod = paymentperiod;
   }

        public Date getActualcancellationdate() {
      return this.actualcancellationdate;
   }

   public void setActualcancellationdate(Date actualcancellationdate) {
      this.actualcancellationdate = actualcancellationdate;
   }

   public Boolean getEbanking() {
      return this.ebanking;
   }

   public void setEbanking(Boolean ebanking) {
      this.ebanking = ebanking;
   }

   public BigDecimal getDunningcosts() {
      return this.dunningcosts;
   }

   public void setDunningcosts(BigDecimal dunningcosts) {
      this.dunningcosts = dunningcosts;
   public Date getDunningdate() {
      return this.dunningdate;
   }

   public void setDunningdate(Date dunningdate) {
      this.dunningdate = dunningdate;
   }

   public String getCustomerip() {
      return this.customerip;
   }

   public void setCustomerip(String customerip) {
      this.customerip = customerip;
   }

   public BigDecimal getNetsubtotal() {
      return this.netsubtotal;
   }

   public void setNetsubtotal(BigDecimal netsubtotal) {
      this.netsubtotal = netsubtotal;
   }

   public String getRemarks() {
      return this.remarks;
   }

   public void setRemarks(String remarks) {
      this.remarks = remarks;
   }

   public Set getOrderitems() {
      return this.orderitems;
   }

   public void setOrderitems(Set orderitems) {
      this.orderitems = orderitems;
   }

   public Vat getVat() {
      return this.vat;
   }

   public void setVat(Vat vat) {
      this.vat = vat;
   }

   public Customer getCustomer() {
      return this.customer;
   }

   public void setCustomer(Customer customer) {
      this.customer = customer;
   }

   public Reseller getReseller() {
      return this.reseller;
   }

   public void setReseller(Reseller reseller) {
      this.reseller = reseller;
   }

   public Account getAccount() {
      return this.account;
   }

   public void setAccount(Account account) {
      this.account = account;
   }

   public Invoices getInvoice() {
      return this.invoice;
   }

   public void setInvoice(Invoices invoice) {
      this.invoice = invoice;
   }

        public String getCurrency() {
                return currency;
        }

        public void setCurrency(String currency) {
                this.currency = currency;
        }

   public Long getCampaignid() {
      return campaignid;
   }

   public void setCampaignid(Long campaignid) {
      this.campaignid = campaignid;
   }

}


it prints:
Quote:
11:03:05,042 ERROR [STDERR] ~~~~ 9433 ~~~~
11:03:05,042 ERROR [STDERR] ~~~~ 3 ~~~~


from postgreSQL:
Code:
select * from t_order where orderid=9433;
orderid |       lastupdated       |        status         | txhandle | parentorderid | processinstanceid |       billingdate       | billingamount | netbillingamount | paymentperiod | actualcancellationdate | ebanking | dunningcosts | dunningdate |  customerip  | netsubtotal | remarks | vatid | contactid | resellerid | accountid | invoiceid | portalid | currency | campaignid
---------+-------------------------+-----------------------+----------+---------------+-------------------+-------------------------+---------------+------------------+---------------+------------------------+----------+--------------+-------------+--------------+-------------+---------+-------+-----------+------------+-----------+-----------+----------+----------+------------
    9433 | 2006-12-14 11:03:55.376 | ORDER_WAITING_PAYMENT |          |               |                   | 2006-12-14 11:03:43.158 |         64.87 |            54.06 |               |                        |          |              |             | 62.116.8.105 |       54.06 |         |     1 |     15577 |            |      7206 |      5197 |        3 | EUR      |
(1 row)


Top
 Profile  
 
 Post subject: update
PostPosted: Thu Dec 14, 2006 6:42 am 
Newbie

Joined: Wed Dec 13, 2006 12:15 pm
Posts: 6
after reading http://www.hibernate.org/116.html#A6 I added

Code:
sess.flush();


to my function, but it didn't help...

Thanks for all the replys BTW, and sorry I didn't post the code in the first place.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 7:53 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, in your code, I still can't see any tx management :
Quote:
Code:
Session sess = HibernateSessionFactory.getCurrentSession();
    <snip>.db.mappings.TOrder hOrder = new <snip>.db.mappings.TOrder();
    CopyBeanUtil.copy(order, hOrder);
    hOrder.setCustomer(loadHCustomer(customerid));
    hOrder.setVat(loadHVat(order.getVatid()));
    hOrder.setReseller(loadHReseller(order.getResellerid()));
    hOrder.setAccount(loadHAccount(order.getAccountid()));
    hOrder.setCampaignid(order.getCampaignid() );

    Long result = (Long) sess.save(hOrder);
         
    System.err.println("~~~~ "+hOrder.getOrderid()+" ~~~~");
    System.err.println("~~~~ "+hOrder.getCampaignid()+" ~~~~");

    return result;


Where is your beginTransaction() and commit() located ? Did you do it somewhere else ? IMO, relying on a flush() to see the objects in the db is highly dangerous. I think it uses somethink like autocommit mode, but I guess it also depends completely on the db you're facing.

Remember that
Quote:
There can be no communication with a database outside of a database transaction.
, see http://www.hibernate.org/42.html .

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 8:03 am 
Newbie

Joined: Wed Dec 13, 2006 12:15 pm
Posts: 6
I'm a bit at a loss regarding this code base (to which I'm rather new).

I will try adding
Code:
sess.beginTransaction();
after
Code:
Session sess = HibernateSessionFactory.getCurrentSession();


and
Code:
sess.getTransaction().commit();
instead of the flush, and see if that helps.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 8:19 am 
Newbie

Joined: Wed Dec 13, 2006 12:15 pm
Posts: 6
that didn't help- it is exactly as it was before. What's interesting is that some of the other fields that are updated in that function _do_ get updated in postgreSQL


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 8:41 am 
Newbie

Joined: Wed Dec 13, 2006 12:15 pm
Posts: 6
I solved it - thanks for the help!

The problem was that I was changing the java file for the mapping but not the hbm.xml file.


Ido.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 11:13 am 
Newbie

Joined: Fri Dec 09, 2005 4:57 am
Posts: 7
Idoido,

I am glad that your problem got resolved. Mine is a little stickier. I am able create a new record in the database, but not able to update the record using session.update() method.

I was wondering if anyone can comment on why the updates are not being saved to the database, based on the code and error log posted in my note in this thread.


Thanks

Rauf


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.