-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to Insert values through HQL Insert
PostPosted: Wed Jun 02, 2010 4:09 am 
Newbie

Joined: Wed Jun 02, 2010 3:47 am
Posts: 3
Can any one of you help me.I have read that HQL only supports insert from another table.But in my case , there is no other table to select from.My query is something like this
Code:
"insert into TGbOrganizationMaster(COrgId,COrgName,COrgAddress)values('" + orgid + "','" + orgName + "','" + orgAddress + "')";

can any body tell me, how to insert this values using hibernate?


Top
 Profile  
 
 Post subject: Re: How to Insert values through HQL Insert
PostPosted: Wed Jun 02, 2010 4:57 am 
Beginner
Beginner

Joined: Thu Feb 08, 2007 10:40 am
Posts: 46
You just create your Java object and save it. Hibernate will generate the required SQL for you and will insert it into the mapped table.

For example:
Code:
TGbOrganizationMaster organizationMaster = new TGbOrganizationMaster();
organizationMaster.setOrdId(123);
organizationMaster.setOrdName("ACME Labs");
organizationMaster.setOrgAddress("Milky Way");
session.saveOrUpdate(organizationMaster);


Top
 Profile  
 
 Post subject: Re: How to Insert values through HQL Insert
PostPosted: Wed Jun 02, 2010 6:00 am 
Newbie

Joined: Wed Jun 02, 2010 3:47 am
Posts: 3
Thanks for the reply .I tried that,But my problem here is,i have two classes for the table t_gb_OrganizationMaster.
(1)TGbOrganizationMaster and( 2)TGbOrganizationMasterId is having orgId,stateId,reginId .So i tried something like this.
Code:
 
TGbOrganizationMaster hai = new TGbOrganizationMaster();
       TGbOrganizationMasterId tgbid = new TGbOrganizationMasterId();   
             HashSet set = new HashSet();
                     tgbid.setCOrgId(orgid);
                    tgbid.setCStateId(stateId);
                    tgbid.setCRegionId(regionId);
                    hai.setCOrgName(orgName);
                    hai.setCOrgAddress(orgAddress);
                    set.add(tgbid);
                    set.add(hai);
                  session.save(set);
               

But i am getting error something like this
Quote:
ERROR [STDERR] org.hibernate.MappingException: Unknown entity: org.hsqldb.lib.HashSet
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187


To use set, i imported
Code:
import org.hsqldb.lib.HashSet;
.
Please give a solution for this.


Top
 Profile  
 
 Post subject: Re: How to Insert values through HQL Insert
PostPosted: Wed Jun 02, 2010 6:29 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Several problems.
1) you shouldn't use org.hsqldb.lib.HashSet.
Use java.utils.collections.Set when you need to define a collection with unique elements.

2) you have to pass entities to session.save, not other objects.

3) to have two different classes mapped to a single table, learn how to do the mapping. It's important that you learn to map tables/entities before using hibernate.

_________________
Sanne
http://in.relation.to/


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