Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ritchie Warsi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic