-->
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: Insert if not exist
PostPosted: Sat Aug 07, 2010 12:23 pm 
Newbie

Joined: Sat Aug 07, 2010 12:12 pm
Posts: 2
I am using Fluent NHibernate to define the models and mappings. I am also fairly new to hibernate so please pardon my ignorance :)

For an object like one below:
> Entity
- int ID (Identity)
- string Name
- OtherInfo
> Attribute
- int ID
- string Name (Unique Key)
- Other info
> Entity_Attribute
- int ID
- int EntityID (FK to Entity)
- AttributeID (FK to Attribute)
- Value

I have the mappings well defined and I am able add new entities, attributes and entity_attribute. But what I was wondering was if there is a way (fluent/interceptor/listener) to add an object (such as attribute or entity_attribute) if it doesn't exist in database otherwise reference the existing one.

Something which can represented by following query:

> Attribute query
if exists(select id from attribute where name=@Name)
begin
insert attribute query
select scope_Identity()
end
else
begin
select attribute's id
end
> Entity Attribute query
if exists (select id from entity_attribute where attributeid=@attributeid and entityid=@entityid)
begin
insert entity_attribute query
select scope_identity
end
else
begin
update entity_attribute query
select id from entity_attribute
end


Top
 Profile  
 
 Post subject: Re: Insert if not exist
PostPosted: Tue Aug 10, 2010 10:00 pm 
Newbie

Joined: Sat Aug 07, 2010 12:12 pm
Posts: 2
Ignore. I figured it out myself.


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.