Struts2-Hibernate Annotations: Mapping tables and child tables using foreign keys

291 Views Asked by At

Everyone has been really helpful as I try to learn Struts2 with Hibernate using Oracle in my conversion project and I really appreciate it. I've hit a new snag which I'm hoping is my last before completing my project. My database already exists so I'm trying not to get my DBA involved.

I have a form that allows a user to query products from the products table via product_name. I need to display the products found along with their corresponding pricing data located in the pricing table. The relationship is one-to-many in that for every row in products, there can be zero to many corresponding rows in the pricing table.

 product table     pricing table
 PRODUCT_NAME      PRODUCT_NAME
 STATE_CD          STATE_CD
 column3           RATE_TYPE
 column4           RATE
 column5
 column6

The product table has a composite key primary key (product_pk) of product_name and state_cd. The pricing table has a composite primary key (pricing_pk) of product_name, state_cd, and rate_type. The pricing table also has a composite foreign key (product_pricing_fk) of product and state_cd.

I've been reading through a bunch of pages on how to set this up but I feel more confused than when I started. I'm basically at a loss as to how to set this up using annotations.

Question #1 - I currently have a Product.java class with getters and setters for every field and it's displaying my results correctly. What I don't have correct is how I indicate that the table has a composite key.

Question #2 - Do I need to create a separate class for my pricing table with getters and setters for every field in my pricing table or do these fields go in Product.java?

Question #3 - How do I tell it about the foreign key?

This is my current Product.java.

 package com.memo.model;

 import java.io.Serializable;
 import java.sql.Date;
 import java.util.List;
 import java.util.ArrayList;

 import javax.persistence.Id;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Table;

 @Entity
 @Table(name = "Product")
 public class Product implements Serializable {

     private static final long serialVersionUID = 1L;

     private String productName;
     private String stateCd;
     private String column3;

         @Id
     @Column(name = "PRODUCT_NAME")
     public String getProductName() {
             return productName;
     }

     @Id
     @Column(name = "STATE_CD")
     public String getStateCd() {
     return stateCd;
     }

     @Column(name = "COLUMN3")
     public String getColumn3() {
     return column3;
     }

     ....

     public void setProduct(String product) {
             this.product = product;
     }

     public void setStateCd(String stateCd) {
     this.stateCd = stateCd;
     }

     public void setColumn3(String column3) {
     this.column3 = column3;
     }

     ...

Any help would be appreciated!!!

0

There are 0 best solutions below