-->
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.  [ 3 posts ] 
Author Message
 Post subject: Determining if a Unique Value exists...
PostPosted: Fri Sep 09, 2005 9:49 am 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Entity" table="ENTITY">
    <cache usage="read-write" />
    <id name="id" column="ID" unsaved-value="-1">
      <generator class="increment"></generator>
    </id>
    <set name="stringAttributes" table="ATTRIBUTERELATIONSHIP"
    lazy="false" cascade="all-delete-orphan"
    where="DISCRIMINATOR = 1" fetch="join">
      <key column="ENTITYID" not-null="true"></key>
      <one-to-many class="StringAttribute" />
    </set>
  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Attribute"
  table="ATTRIBUTERELATIONSHIP" discriminator-value="-1"
  batch-size="50">
    <cache usage="read-write" />
    <id name="id" column="ID" unsaved-value="-1">
      <generator class="increment"></generator>
    </id>
    <discriminator column="DISCRIMINATOR" not-null="true"
    force="true" />
    <many-to-one name="attributeClass"
    class="AttributeClass" column="ATTRIBUTECLASSID"
    cascade="save-update" lazy="false">
    </many-to-one>
    <subclass name="StringAttribute" discriminator-value="1"
    batch-size="50">
      <many-to-one name="stringValue"
      class="StringValue" column="VALUEID" not-null="true" unique="true"
      cascade="persist,merge,save-update" lazy="false">
      </many-to-one>
    </subclass>
  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="StringValue"
  table="STRINGATTRIBUTE" batch-size="50">
    <cache usage="read-write" />
    <id name="id" column="ID" unsaved-value="-1">
      <generator class="increment"></generator>
    </id>
    <property name="value" column="VALUE"></property>
  </class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
session.saveOrUpdate(entity);

Name and version of the database you are using:
HSQLDB 1.8.0

The generated SQL (show_sql=true):
Hibernate: insert into ENTITY (ID) values (?)
Hibernate: insert into STRINGATTRIBUTE (VALUE, ID) values (?, ?)

I am having trouble figuring out how to map my classes to see if a unique value exists for a given class.

As you can see from my mapping, i have a class Entity that has a Set (one-to-many) of StringAttributes. Each StringAttribute has a mapping (many-to-one) to a single StringValue object. The problem I am having is that StringValue's value column is unique, and I don't know how to tell hibernate to check for the existence of this value before attempting an insert.

Help would be greatly appreciated (credits!)
Jason


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 10:43 am 
Beginner
Beginner

Joined: Tue Jun 28, 2005 4:33 pm
Posts: 21
If the value column in STRINGATTRIBUTE is unique, could you use a composite-id? You'd probably have to throw in an Interceptor to see if the object was persisted or not, but that would solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 11:03 am 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
I don't think composite-id is the way to go, but i could be convinced otherwise...

An interceptor may work well here, and i'm using spring so interceptors are super easy to add in.

Thanks for the suggestion,
Jason


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