aspose file tools*
The moose likes Object Relational Mapping and the fly likes [Hibernate/Struts] Inserting data in table with foreign/composite key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "[Hibernate/Struts] Inserting data in table with foreign/composite key" Watch "[Hibernate/Struts] Inserting data in table with foreign/composite key" New topic
Author

[Hibernate/Struts] Inserting data in table with foreign/composite key

Ritchie Warsi
Greenhorn

Joined: Nov 02, 2004
Posts: 29
I have some other questions regarding Hibernate and Struts. The Hibernate documention has some examples but I cannot find any good examples of use with a struts webapplication, so I hope someone here knows.

I've got 2 tables, say Customer with PK Customer ID and other table UserAccount with composite PK for Username, Password & FK Customer ID.
I have to create a new User Account, filling in a form on a jsp page with 3 fields, username, password & the customer id which the login will belong to.
How do I set up my business and action for this?

I know how to insert into one table (simple insertion), but this is a little bit different.
My businness class should have something like a public void addUserAccount(String username, String password, Customer customerid) thing I reckon?
But what further? Action class etc etc.

If someone can post an example I'd appreciate it!
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
When using hibernate, I found it best to think about my class structure before my database structure. Take a step back, and don't worry about primary and foreign keys (hiberate will do it for you).

Instead, think about your entity classes, and the relationships between them. That'll lead to your relationship mapping, which will lead to your db schema.
Ritchie Warsi
Greenhorn

Joined: Nov 02, 2004
Posts: 29
Allright, I have no other way but to handle it like it was.
My boss makes the datamodel and the database (MySQL) and I let hibernate map the tables and generate the pojo classes.
So it's not up to me unfortunately to think about the structure.

So for instance I got following tables
______________ _____________ ___________
|User_Account| |User_Customer| |Customer |
-------------- ------------- ------------
|Username | |Username | |CustomerID|
|Password | |CustomerID | |Etc etc |
|Role | --------------- ------------
--------------

CustomerID and Username being "foreign" in User_Customer table, Hibernate generates a UserCustomer.java file with id:
So there is another class called UserCustomerKey that consists of

So as you can see, me being a struts n00b, this is very confusing. The Hibernate docs is, in my opinion not really clear on how to actually use this with CRUD operations for instance. (In other words, the doc isn't really n00b friendly i think).

So does anyone know how to do an Insert for instance in this kind of situation? I'm doing all my CRUD related operatotions in a business class and let the action class call those. But yet I have no idea how it should look like (Simple inserts and such I can do).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Hibernate/Struts] Inserting data in table with foreign/composite key