How to xml-code a hibernate one-to-one?

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

How to xml-code a hibernate one-to-one?

TheStijn
Hi,

I have a table user and a table customer which can be linked by a one-to-one relationship; meaning a user can reference 1 customer and vice-versa.

I'm a bit stuck on how to set for instance a customer on a user since this customer is not defined yet:

<user user_id="1" ... customer_id="2" />

<customer customer_id="2" ... user_id="1" />


DBUnit throws an SQL exception when trying to insert the user since it references a customer that was not created yet. The error is logical since there is a DB constraint on this field.

My question is, how to go around this problem with DBUnit in XML without dropping the DB constraint obviously and if possible also without doing a 'manual' update of user in my unit test?

many thanks,
Stijn
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Dave S-B
Presumably your application (i.e. Hibernate) manages to do this, so
you'd have to get DBUnit to do it in the same way. I can only think of
two ways;

1. Disable the constraints whilst you load the data. This is what I do
all the time. The SQL will be specific to your database, but basically,
disable constraints before you do the DBUnit load, then enable them
afterwards.

2. Make it a 2-stage process in the DBUnit XML file. I'm assuming that
you can add records into the user table with a NULL customer_id. So
remove all the customer_ids from the user XML and have that first in the
XML, then have the customer XML, then have a copy of the user XML *with*
the customer_id in. Using a REFRESH operation the first lot of user data
will get inserted (no customer_id), then the customer data, then the
user data will get updated with the customer_ids included. Not very
pretty, but I believe it'll work.

Dave S-B
Lledr Solutions

TheStijn wrote:

> Hi,
>
> I have a table user and a table customer which can be linked by a one-to-one
> relationship; meaning a user can reference 1 customer and vice-versa.
>
> I'm a bit stuck on how to set for instance a customer on a user since this
> customer is not defined yet:
>
> <user user_id="1" ... customer_id="2" />
>
> <customer customer_id="2" ... user_id="1" />
>
> DBUnit throws an SQL exception when trying to insert the user since it
> references a customer that was not created yet. The error is logical since
> there is a DB constraint on this field.
>
> My question is, how to go around this problem with DBUnit in XML without
> dropping the DB constraint obviously and if possible also without doing a
> 'manual' update of user in my unit test?
>
> many thanks,
> Stijn
>  
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.375 / Virus Database: 270.12.92/2203 - Release Date: 06/26/09 05:53:00
>
>  


------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

TheStijn
thanks for the suggestions.

I looked for disabling the constraints and found solutions for MySQL and such (adding &sessionVariables=FOREIGN_KEY_CHECKS=0 to the URL). However, I am using HSQLDB and this solution does not work. Do you have any idea on how to manage this for HSQLDB?

Your second sounds promising but I have to admit that I have no idea how to implement this in XML.
How can you update an existing row with dbunit?

tx again,
Stijn

Dave S-B wrote
Presumably your application (i.e. Hibernate) manages to do this, so
you'd have to get DBUnit to do it in the same way. I can only think of
two ways;

1. Disable the constraints whilst you load the data. This is what I do
all the time. The SQL will be specific to your database, but basically,
disable constraints before you do the DBUnit load, then enable them
afterwards.

2. Make it a 2-stage process in the DBUnit XML file. I'm assuming that
you can add records into the user table with a NULL customer_id. So
remove all the customer_ids from the user XML and have that first in the
XML, then have the customer XML, then have a copy of the user XML *with*
the customer_id in. Using a REFRESH operation the first lot of user data
will get inserted (no customer_id), then the customer data, then the
user data will get updated with the customer_ids included. Not very
pretty, but I believe it'll work.

Dave S-B
Lledr Solutions

TheStijn wrote:
> Hi,
>
> I have a table user and a table customer which can be linked by a one-to-one
> relationship; meaning a user can reference 1 customer and vice-versa.
>
> I'm a bit stuck on how to set for instance a customer on a user since this
> customer is not defined yet:
>
> <user user_id="1" ... customer_id="2" />
>
> <customer customer_id="2" ... user_id="1" />
>
> DBUnit throws an SQL exception when trying to insert the user since it
> references a customer that was not created yet. The error is logical since
> there is a DB constraint on this field.
>
> My question is, how to go around this problem with DBUnit in XML without
> dropping the DB constraint obviously and if possible also without doing a
> 'manual' update of user in my unit test?
>
> many thanks,
> Stijn
>  
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.375 / Virus Database: 270.12.92/2203 - Release Date: 06/26/09 05:53:00
>
>  


------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
dbunit-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Dave S-B
In HSQLDB you can do the following in your setup();

dbUnitConnection.getConnection().prepareStatement("set
referential_integrity FALSE").execute();

...where dbUnitConnection is the DBUnit IConnection. Then do the load
using a database operation (e.g. DatabaseOperation.REFRESH). Then do this...

dbUnitConnection.getConnection().prepareStatement("set
referential_integrity TRUE").execute();

The second idea just involves making a normal DBUnit XML dataset file
(see http://www.dbunit.org/components.html#FlatXmlDataSet) and then
copying the XML for the user data to the end and modifying it by hand
(or search & replace). If you use a REFRESH operation then DBUnit will
insert data if it's not already there (based on the primary key of the
database table) or update the row if the primary key already exists.

Dave S-B

TheStijn wrote:

> thanks for the suggestions.
>
> I looked for disabling the constraints and found solutions for MySQL and
> such (adding &amp;sessionVariables=FOREIGN_KEY_CHECKS=0 to the URL).
> However, I am using HSQLDB and this solution does not work. Do you have any
> idea on how to manage this for HSQLDB?
>
> Your second sounds promising but I have to admit that I have no idea how to
> implement this in XML.
> How can you update an existing row with dbunit?
>
> tx again,
> Stijn
>
>
> Dave S-B wrote:
>  
>> Presumably your application (i.e. Hibernate) manages to do this, so
>> you'd have to get DBUnit to do it in the same way. I can only think of
>> two ways;
>>
>> 1. Disable the constraints whilst you load the data. This is what I do
>> all the time. The SQL will be specific to your database, but basically,
>> disable constraints before you do the DBUnit load, then enable them
>> afterwards.
>>
>> 2. Make it a 2-stage process in the DBUnit XML file. I'm assuming that
>> you can add records into the user table with a NULL customer_id. So
>> remove all the customer_ids from the user XML and have that first in the
>> XML, then have the customer XML, then have a copy of the user XML *with*
>> the customer_id in. Using a REFRESH operation the first lot of user data
>> will get inserted (no customer_id), then the customer data, then the
>> user data will get updated with the customer_ids included. Not very
>> pretty, but I believe it'll work.
>>
>> Dave S-B
>> Lledr Solutions
>>
>> TheStijn wrote:
>>    
>>> Hi,
>>>
>>> I have a table user and a table customer which can be linked by a
>>> one-to-one
>>> relationship; meaning a user can reference 1 customer and vice-versa.
>>>
>>> I'm a bit stuck on how to set for instance a customer on a user since
>>> this
>>> customer is not defined yet:
>>>
>>> <user user_id="1" ... customer_id="2" />
>>>
>>> <customer customer_id="2" ... user_id="1" />
>>>
>>> DBUnit throws an SQL exception when trying to insert the user since it
>>> references a customer that was not created yet. The error is logical
>>> since
>>> there is a DB constraint on this field.
>>>
>>> My question is, how to go around this problem with DBUnit in XML without
>>> dropping the DB constraint obviously and if possible also without doing a
>>> 'manual' update of user in my unit test?
>>>
>>> many thanks,
>>> Stijn
>>>  
>>> ------------------------------------------------------------------------
>>>
>>>
>>> No virus found in this incoming message.
>>> Checked by AVG - www.avg.com
>>> Version: 8.5.375 / Virus Database: 270.12.92/2203 - Release Date:
>>> 06/26/09 05:53:00
>>>
>>>  
>>>      
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> dbunit-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>
>>
>>    
>
>  
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.375 / Virus Database: 270.12.92/2203 - Release Date: 06/26/09 05:53:00
>
>  


------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Matthias Gommeringer
In reply to this post by TheStijn
Hi Stijn,

did you also have a look at the DatabaseOperation.TRANSACTION? It should allow to insert a complete dataset within one jdbc transaction. This way you wouldn't have to disable any of your constraints.

DatabaseOperation.TRANSACTION(DatabaseOperation.INSERT).execute(connection, dataSet);

You probably have to take care of the dbunit Batch-Statements which write to the db after reaching the batch size. Therefore you might need to provide a custom IStatementFactory (DatabaseConfig.PROPERTY_STATEMENT_FACTORY) which does not execute the batch for each table but only once at the very end. Maybe the dbunit SimplePreparedStatement would do the job?

Please feel free to play around with my above thoughts and don't be angry if they won't work cause I did not test that stuff...

regards,
matthias


> -----Ursprüngliche Nachricht-----
> Von: "Dave S-B" <[hidden email]>
> Gesendet: 26.06.09 23:53:45
> An: [hidden email]
> Betreff: Re: [dbunit-user] How to xml-code a hibernate one-to-one?


> In HSQLDB you can do the following in your setup();
>
> dbUnitConnection.getConnection().prepareStatement("set
> referential_integrity FALSE").execute();
>
> ...where dbUnitConnection is the DBUnit IConnection. Then do the load
> using a database operation (e.g. DatabaseOperation.REFRESH). Then do this...
>
> dbUnitConnection.getConnection().prepareStatement("set
> referential_integrity TRUE").execute();
>
> The second idea just involves making a normal DBUnit XML dataset file
> (see http://www.dbunit.org/components.html#FlatXmlDataSet) and then
> copying the XML for the user data to the end and modifying it by hand
> (or search & replace). If you use a REFRESH operation then DBUnit will
> insert data if it's not already there (based on the primary key of the
> database table) or update the row if the primary key already exists.
>
> Dave S-B
>
> TheStijn wrote:
> > thanks for the suggestions.
> >
> > I looked for disabling the constraints and found solutions for MySQL and
> > such (adding &amp;sessionVariables=FOREIGN_KEY_CHECKS=0 to the URL).
> > However, I am using HSQLDB and this solution does not work. Do you have any
> > idea on how to manage this for HSQLDB?
> >
> > Your second sounds promising but I have to admit that I have no idea how to
> > implement this in XML.
> > How can you update an existing row with dbunit?
> >
> > tx again,
> > Stijn
> >
> >
> > Dave S-B wrote:
> >  
> >> Presumably your application (i.e. Hibernate) manages to do this, so
> >> you'd have to get DBUnit to do it in the same way. I can only think of
> >> two ways;
> >>
> >> 1. Disable the constraints whilst you load the data. This is what I do
> >> all the time. The SQL will be specific to your database, but basically,
> >> disable constraints before you do the DBUnit load, then enable them
> >> afterwards.
> >>
> >> 2. Make it a 2-stage process in the DBUnit XML file. I'm assuming that
> >> you can add records into the user table with a NULL customer_id. So
> >> remove all the customer_ids from the user XML and have that first in the
> >> XML, then have the customer XML, then have a copy of the user XML *with*
> >> the customer_id in. Using a REFRESH operation the first lot of user data
> >> will get inserted (no customer_id), then the customer data, then the
> >> user data will get updated with the customer_ids included. Not very
> >> pretty, but I believe it'll work.
> >>
> >> Dave S-B
> >> Lledr Solutions
> >>
> >> TheStijn wrote:
> >>    
> >>> Hi,
> >>>
> >>> I have a table user and a table customer which can be linked by a
> >>> one-to-one
> >>> relationship; meaning a user can reference 1 customer and vice-versa.
> >>>
> >>> I'm a bit stuck on how to set for instance a customer on a user since
> >>> this
> >>> customer is not defined yet:
> >>>
> >>> <user user_id="1" ... customer_id="2" />
> >>>
> >>> <customer customer_id="2" ... user_id="1" />
> >>>
> >>> DBUnit throws an SQL exception when trying to insert the user since it
> >>> references a customer that was not created yet. The error is logical
> >>> since
> >>> there is a DB constraint on this field.
> >>>
> >>> My question is, how to go around this problem with DBUnit in XML without
> >>> dropping the DB constraint obviously and if possible also without doing a
> >>> 'manual' update of user in my unit test?
> >>>
> >>> many thanks,
> >>> Stijn
> >>>  
> >>> ------------------------------------------------------------------------
> >>>
> >>>
> >>> No virus found in this incoming message.
> >>> Checked by AVG - www.avg.com
> >>> Version: 8.5.375 / Virus Database: 270.12.92/2203 - Release Date:
> >>> 06/26/09 05:53:00
> >>>
> >>>  
> >>>      
> >> ------------------------------------------------------------------------------
> >> _______________________________________________
> >> dbunit-user mailing list
> >> [hidden email]
> >> https://lists.sourceforge.net/lists/listinfo/dbunit-user
> >>
> >>
> >>    
> >
> >  
> > ------------------------------------------------------------------------
> >
> >
> > No virus found in this incoming message.
> > Checked by AVG - www.avg.com
> > Version: 8.5.375 / Virus Database: 270.12.92/2203 - Release Date: 06/26/09 05:53:00
> >
> >  
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>


______________________________________________________
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de


------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

TheStijn
In reply to this post by Dave S-B
thanks Dave,

I think I prefer the first solution < less XML editing.
But I'm a bit stuck on how to implement this exactly in conjunction with Spring.

This is the way I write my test classes:

@SpringApplicationContext( {
                "classpath:com/m4w/common/props/spring/common-data-test.xml",
                "classpath:com/m4w/common/props/spring/common-dao.xml" })
@DataSet
@RunWith(UnitilsJUnit4TestClassRunner.class)
@Transactional(TransactionMode.ROLLBACK)
public class UserDAOTest extends AbstractHibernateDAOTest {

    @SpringBeanByType
    private UserDAO userDao;

    @Test
    public void getAll() throws Exception {
        final List<User> result = this.userDao.getAll();
        assertEquals(3, result.size());
    }
}

So, how can I implement either solutions without completely refactoring the way my unit tests are created?

thanks,
Stijn

Dave S-B wrote
In HSQLDB you can do the following in your setup();

dbUnitConnection.getConnection().prepareStatement("set
referential_integrity FALSE").execute();

...where dbUnitConnection is the DBUnit IConnection. Then do the load
using a database operation (e.g. DatabaseOperation.REFRESH). Then do this...

dbUnitConnection.getConnection().prepareStatement("set
referential_integrity TRUE").execute();

The second idea just involves making a normal DBUnit XML dataset file
(see http://www.dbunit.org/components.html#FlatXmlDataSet) and then
copying the XML for the user data to the end and modifying it by hand
(or search & replace). If you use a REFRESH operation then DBUnit will
insert data if it's not already there (based on the primary key of the
database table) or update the row if the primary key already exists.

Dave S-B
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Dave S-B
Well, without seeing how the whole thing is structured, it's hard to
tell, but somewhere or other there will be methods for doing the setup()
and teardown() for each test (probably in that AbstractHibernateDAOTest,
but maybe higher in the chain). Either way, setup() is where the data
will be loaded, so override/change that so the constraint disabling
happens first, then the database operations, then constraint enabling at
the end.

Dave S-B

TheStijn wrote:

> thanks Dave,
>
> I think I prefer the first solution < less XML editing.
> But I'm a bit stuck on how to implement this exactly in conjunction with
> Spring.
>
> This is the way I write my test classes:
>
> @SpringApplicationContext( {
>                
> "classpath:com/m4w/common/props/spring/common-data-test.xml",
>                 "classpath:com/m4w/common/props/spring/common-dao.xml" })
> @DataSet
> @RunWith(UnitilsJUnit4TestClassRunner.class)
> @Transactional(TransactionMode.ROLLBACK)
> public class UserDAOTest extends AbstractHibernateDAOTest {
>
>     @SpringBeanByType
>     private UserDAO userDao;
>
>     @Test
>     public void getAll() throws Exception {
>         final List<User> result = this.userDao.getAll();
>         assertEquals(3, result.size());
>     }
> }
>
> So, how can I implement either solutions without completely refactoring the
> way my unit tests are created?
>
> thanks,
> Stijn
>
>
> Dave S-B wrote:
>  
>> In HSQLDB you can do the following in your setup();
>>
>> dbUnitConnection.getConnection().prepareStatement("set
>> referential_integrity FALSE").execute();
>>
>> ...where dbUnitConnection is the DBUnit IConnection. Then do the load
>> using a database operation (e.g. DatabaseOperation.REFRESH). Then do
>> this...
>>
>> dbUnitConnection.getConnection().prepareStatement("set
>> referential_integrity TRUE").execute();
>>
>> The second idea just involves making a normal DBUnit XML dataset file
>> (see http://www.dbunit.org/components.html#FlatXmlDataSet) and then
>> copying the XML for the user data to the end and modifying it by hand
>> (or search & replace). If you use a REFRESH operation then DBUnit will
>> insert data if it's not already there (based on the primary key of the
>> database table) or update the row if the primary key already exists.
>>
>> Dave S-B
>>
>>
>>    
>
>  
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.375 / Virus Database: 270.12.94/2208 - Release Date: 06/29/09 05:54:00
>
>  


------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Luiz Eduardo Guida Valmont-2
In reply to this post by TheStijn
Since your user id (uniquely) defines the customer just as the customer id (uniquely) defines the user, you can also merge both entities. Whether that is acceptable or not is a domain-specific problem.

2009/6/26 TheStijn <[hidden email]>

Hi,

I have a table user and a table customer which can be linked by a one-to-one
relationship; meaning a user can reference 1 customer and vice-versa.

I'm a bit stuck on how to set for instance a customer on a user since this
customer is not defined yet:

<user user_id="1" ... customer_id="2" />

<customer customer_id="2" ... user_id="1" />

DBUnit throws an SQL exception when trying to insert the user since it
references a customer that was not created yet. The error is logical since
there is a DB constraint on this field.

My question is, how to go around this problem with DBUnit in XML without
dropping the DB constraint obviously and if possible also without doing a
'manual' update of user in my unit test?

many thanks,
Stijn
--
View this message in context: http://www.nabble.com/How-to-xml-code-a-hibernate-one-to-one--tp24217768p24217768.html
Sent from the DBUnit - Users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user


------------------------------------------------------------------------------

_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Rusty Wright-2
I was wondering about that as well.

I am by no means any sort of a database guru but the way I'd do the ddl is I wouldn't have the customer_id in the user table or the user_id in the customer table; I'd have a join table that has 2 columns, both foreign keys, user_id and customer_id.  You can specify a unique constraint on each column by itself which will enforce the 1-to-1 business rule.  I'd guess that that will be easier to manage, both when working with the database and with hibernate.


Luiz Eduardo Guida Valmont wrote:

> Since your user id (uniquely) defines the customer just as the customer
> id (uniquely) defines the user, you can also merge both entities.
> Whether that is acceptable or not is a domain-specific problem.
>
> 2009/6/26 TheStijn <[hidden email]
> <mailto:[hidden email]>>
>
>
>     Hi,
>
>     I have a table user and a table customer which can be linked by a
>     one-to-one
>     relationship; meaning a user can reference 1 customer and vice-versa.
>
>     I'm a bit stuck on how to set for instance a customer on a user
>     since this
>     customer is not defined yet:
>
>     <user user_id="1" ... customer_id="2" />
>
>     <customer customer_id="2" ... user_id="1" />
>
>     DBUnit throws an SQL exception when trying to insert the user since it
>     references a customer that was not created yet. The error is logical
>     since
>     there is a DB constraint on this field.
>
>     My question is, how to go around this problem with DBUnit in XML without
>     dropping the DB constraint obviously and if possible also without
>     doing a
>     'manual' update of user in my unit test?
>
>     many thanks,
>     Stijn
>     --
>     View this message in context:
>     http://www.nabble.com/How-to-xml-code-a-hibernate-one-to-one--tp24217768p24217768.html
>     Sent from the DBUnit - Users mailing list archive at Nabble.com.
>
>
>     ------------------------------------------------------------------------------
>     _______________________________________________
>     dbunit-user mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>
>
> ------------------------------------------------------------------------
>
> ------------------------------------------------------------------------------
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user

------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Rusty Wright-2
After re-reading my email I'm now thinking you'd want the unique constraint on the union of both columns customer_id and user_id.  But I'm having trouble visualizing your scenario so I'm not sure if that's right either.  I would think that a customer could consist of multiple users; for example, the customer could be a company and various individuals at that company are users in your database.


Rusty Wright wrote:

> I was wondering about that as well.
>
> I am by no means any sort of a database guru but the way I'd do the ddl
> is I wouldn't have the customer_id in the user table or the user_id in
> the customer table; I'd have a join table that has 2 columns, both
> foreign keys, user_id and customer_id.  You can specify a unique
> constraint on each column by itself which will enforce the 1-to-1
> business rule.  I'd guess that that will be easier to manage, both when
> working with the database and with hibernate.
>
>
> Luiz Eduardo Guida Valmont wrote:
>> Since your user id (uniquely) defines the customer just as the
>> customer id (uniquely) defines the user, you can also merge both
>> entities. Whether that is acceptable or not is a domain-specific problem.
>>
>> 2009/6/26 TheStijn <[hidden email]
>> <mailto:[hidden email]>>
>>
>>
>>     Hi,
>>
>>     I have a table user and a table customer which can be linked by a
>>     one-to-one
>>     relationship; meaning a user can reference 1 customer and vice-versa.
>>
>>     I'm a bit stuck on how to set for instance a customer on a user
>>     since this
>>     customer is not defined yet:
>>
>>     <user user_id="1" ... customer_id="2" />
>>
>>     <customer customer_id="2" ... user_id="1" />
>>
>>     DBUnit throws an SQL exception when trying to insert the user
>> since it
>>     references a customer that was not created yet. The error is logical
>>     since
>>     there is a DB constraint on this field.
>>
>>     My question is, how to go around this problem with DBUnit in XML
>> without
>>     dropping the DB constraint obviously and if possible also without
>>     doing a
>>     'manual' update of user in my unit test?
>>
>>     many thanks,
>>     Stijn
>>     --
>>     View this message in context:
>>    
>> http://www.nabble.com/How-to-xml-code-a-hibernate-one-to-one--tp24217768p24217768.html 
>>
>>     Sent from the DBUnit - Users mailing list archive at Nabble.com.
>>
>>
>>    
>> ------------------------------------------------------------------------------
>>
>>     _______________________________________________
>>     dbunit-user mailing list
>>     [hidden email]
>>     <mailto:[hidden email]>
>>     https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> ------------------------------------------------------------------------------
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> dbunit-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/dbunit-user

------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

Luiz Eduardo Guida Valmont-2
I thought about it too. But that way, it wouldn't be a one-to-one relationship anymore. ;)

customer 1 -> * user

2009/6/29 Rusty Wright <[hidden email]>
After re-reading my email I'm now thinking you'd want the unique constraint on the union of both columns customer_id and user_id.  But I'm having trouble visualizing your scenario so I'm not sure if that's right either.  I would think that a customer could consist of multiple users; for example, the customer could be a company and various individuals at that company are users in your database.


Rusty Wright wrote:
> I was wondering about that as well.
>
> I am by no means any sort of a database guru but the way I'd do the ddl
> is I wouldn't have the customer_id in the user table or the user_id in
> the customer table; I'd have a join table that has 2 columns, both
> foreign keys, user_id and customer_id.  You can specify a unique
> constraint on each column by itself which will enforce the 1-to-1
> business rule.  I'd guess that that will be easier to manage, both when
> working with the database and with hibernate.
>
>
> Luiz Eduardo Guida Valmont wrote:
>> Since your user id (uniquely) defines the customer just as the
>> customer id (uniquely) defines the user, you can also merge both
>> entities. Whether that is acceptable or not is a domain-specific problem.
>>
>> 2009/6/26 TheStijn <[hidden email]
>> <mailto:[hidden email]>>
>>
>>
>>     Hi,
>>
>>     I have a table user and a table customer which can be linked by a
>>     one-to-one
>>     relationship; meaning a user can reference 1 customer and vice-versa.
>>
>>     I'm a bit stuck on how to set for instance a customer on a user
>>     since this
>>     customer is not defined yet:
>>
>>     <user user_id="1" ... customer_id="2" />
>>
>>     <customer customer_id="2" ... user_id="1" />
>>
>>     DBUnit throws an SQL exception when trying to insert the user
>> since it
>>     references a customer that was not created yet. The error is logical
>>     since
>>     there is a DB constraint on this field.
>>
>>     My question is, how to go around this problem with DBUnit in XML
>> without
>>     dropping the DB constraint obviously and if possible also without
>>     doing a
>>     'manual' update of user in my unit test?
>>
>>     many thanks,
>>     Stijn
>>     --
>>     View this message in context:
>>
>> http://www.nabble.com/How-to-xml-code-a-hibernate-one-to-one--tp24217768p24217768.html
>>
>>     Sent from the DBUnit - Users mailing list archive at Nabble.com.
>>
>>
>>
>> ------------------------------------------------------------------------------
>>
>>     _______________________________________________
>>     dbunit-user mailing list
>>     [hidden email]
>>     <mailto:[hidden email]>
>>     https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> ------------------------------------------------------------------------------
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> dbunit-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/dbunit-user

------------------------------------------------------------------------------
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user


------------------------------------------------------------------------------

_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to xml-code a hibernate one-to-one?

TheStijn
In reply to this post by TheStijn

In the end I found the solution was quite straightforward:

@DataSet(loadStrategy = RefreshLoadStrategy.class)

But, I agree with some of the poitns raised:
1. A one2one mapping should be avoided
2. if you do want to reference it both ways use an intermediate table

So, thanks to All!