-->
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: Simple question regarding unique table constraints
PostPosted: Mon Aug 27, 2007 8:05 pm 
Newbie

Joined: Wed May 23, 2007 11:53 am
Posts: 12
I have class A with a field of type B. B is mapped to a table whose rows must be unique. Class B contains two fields: an id (Long) and a String.

I populate a new object of type A (including its B) through a form. If a B of the same name already exists, attempting to save the A causes a DataViolationIntegrityException because of the violation of the unique constraint. It is a requirement that the data be normalized, so I can't get rid of table B and make it a String property or anything like that. I'd like to know if it's possible to have Hibernate check table B automatically and resolve this constraint violation on its own, or if I need to do this by hand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 12:47 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
Can you post your mapping documents and DB schema structure.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 11:18 am 
Newbie

Joined: Wed May 23, 2007 11:53 am
Posts: 12
Sorry, turns out that I actually have a Set of Bs, though I believe the principle behind the question remains the same.

Code:
public class A implements Serializable {
  int id;
  private Set<B> foos;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public Set<B> getFoos() {
    return foos;
  }

  public void setFoo(Set<B> foos) {
    this.foos = foos;
  }
}

public class B implements Serializable {
  int id;
  private String bar;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getBar() {
    return bar;
  }

  public void setBar(String bar) {
    this.bar = bar;
  }
}


Code:
<hibernate-mapping>
  <class name="A" table="as">
 
  <id name="id" type="int">
    <column name="id_a"/>
    <generator class="increment"/>
  </id>

  <set name="foos"
          table="a_foos"
          cascade="save-update">
    <key column="id_a"/>
    <many-to-many class="B" column="id_b"/>
  </set>

  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="B" table="bs">

  <id name="id" type="int">
    <column name="id_b"/>
    <generator class="increment"/>
  </id>

  <property name="bar" type="string">
    <column name="b_bar" not-null="true" unique="true"/>
  </property>

  </class>
<hibernate-mapping>


I suspect that the problem right now might lie in the fact that the unique constraint is only on a single column of B. Is there a simple way to get the desired behavior?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 1:13 pm 
Newbie

Joined: Wed May 23, 2007 11:53 am
Posts: 12
To clarify the problem some more:

I know that this can be solved by the following logic -

Code:
for each foo in a transient A
  attempt to load a B of the same bar; if one exists, use it to replace foo

save A


I was just wondering if there was some way of writing the mapping such that this would be done automatically. Note that if the foo does not exist already, it is to be saved along with the A.


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.