Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Hi i have many to many mapping with extra columns in the join table. table structure look like this.

table vendor{vendor_id, vendor_name, vendor_password, etc...}
table student{student_id, student_name, student_password, etc..}
table test{test_id, test_subject, test_price,test_level, etc..}

Relations as follows

vendor to test --> many-to-many
student to test --> many-to-many

link

table vendor_student_test{vendor_id, student_id, test_id, purchasedDate, assignedDate, writtenDate, result}

i created POJO classes as follows

  1. Vendor.java
public class Vendor {
  private Long vendorId;
  private String vendorName;
  private String vendorPassword;
  private Set<VendorStudentTest> tests;
  private boolean isVendorActivate;
  public boolean isVendorActivate() {
      return isVendorActivate;
  }
  public void setVendorActivate(boolean isVendorActivate) {
      this.isVendorActivate = isVendorActivate;
  }
  public Set<VendorStudentTest> getTests() {
      return tests;
  }
  public void setTests(Set<VendorStudentTest> tests) {
      this.tests = tests;
  }
  public Long getVendorId() {
      return vendorId;
  }
  public void setVendorId(Long vendorId) {
      this.vendorId = vendorId;
  }
  public String getVendorName() {
      return vendorName;
  }
  public void setVendorName(String vendorName) {
      this.vendorName = vendorName;
  }
  public String getVendorPassword() {
      return vendorPassword;
  }
  public void setVendorPassword(String vendorPassword) {
      this.vendorPassword = vendorPassword;
  }

}
  1. Student.java

public class Student { private Long studentId; private String studentName; private String studentPassword; private Set tests; public Set getTests() { return tests; } public void setTests(Set tests) { this.tests = tests; } public Long getStudentId() { return studentId; } public void setStudentId(Long studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentPassword() { return studentPassword; } public void setStudentPassword(String studentPassword) { this.studentPassword = studentPassword; }

}
  1. Test.java
public class Test{
        private Long testId;
  private String testSubject;
  private int testTotalNoOfQuestions;
  private double testPrice;
  public Long getTestId() {
      return testId;
  }
  public void setTestId(Long testId) {
      this.testId = testId;
  }
  public String getTestSubject() {
      return testSubject;
  }
  public void setTestSubject(String testSubject) {
      this.testSubject = testSubject;
  }
  public int getTestTotalNoOfQuestions() {
      return testTotalNoOfQuestions;
  }
  public void setTestTotalNoOfQuestions(int testTotalNoOfQuestions) {
      this.testTotalNoOfQuestions = testTotalNoOfQuestions;
  }
  public double getTestPrice() {
      return testPrice;
  }
  public void setTestPrice(double testPrice) {
      this.testPrice = testPrice;
  }
  public int getTestLevel() {
      return testLevel;
  }
  public void setTestLevel(int testLevel) {
      this.testLevel = testLevel;
  }
  public Set<Question> getTestQuestions() {
      return testQuestions;
  }
  public void setTestQuestions(Set<Question> testQuestions) {
      this.testQuestions = testQuestions;
  }
  private int testLevel;
  private Set<Question> testQuestions;

}
  1. VendorStudentTest.java
public class VendorStudentTest {
  private VendorStudentTestPK vendorStudentTestPK;
  private Date assignedDate;
  private Date purchasedDate;
  private Date writtenDate;
  private double result;
  public VendorStudentTestPK getVendorStudentTestPK() {
      return vendorStudentTestPK;
  }
  public void setVendorStudentTestPK(VendorStudentTestPK vendorStudentTestPK) {
      this.vendorStudentTestPK = vendorStudentTestPK;
  }
  public Date getAssignedDate() {
      return assignedDate;
  }
  public void setAssignedDate(Date assignedDate) {
      this.assignedDate = assignedDate;
  }
  public Date getPurchasedDate() {
      return purchasedDate;
  }
  public void setPurchasedDate(Date purchasedDate) {
      this.purchasedDate = purchasedDate;
  }
  public Date getWrittenDate() {
      return writtenDate;
  }
  public void setWrittenDate(Date writtenDate) {
      this.writtenDate = writtenDate;
  }
  public double getResult() {
      return result;
  }
  public void setResult(double result) {
      this.result = result;
  }

}
  1. VendorStudentTestPK.java
public class VendorStudentTestPK {
  private Vendor vendor;
  private Student student;
  private Test test;

  public VendorStudentTestPK() {
      super();
      // TODO Auto-generated constructor stub
  }
  public VendorStudentTestPK(Vendor vendor, Student student, Test test) {
      super();
      this.vendor = vendor;
      this.student = student;
      this.test = test;
  }
  public Vendor getVendor() {
      return vendor;
  }
  public void setVendor(Vendor vendor) {
      this.vendor = vendor;
  }
  public Student getStudent() {
      return student;
  }
  public void setStudent(Student student) {
      this.student = student;
  }
  public Test getTest() {
      return test;
  }
  public void setTest(Test test) {
      this.test = test;
  }
  @Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((student == null) ? 0 : student.hashCode());
      result = prime * result + ((test == null) ? 0 : test.hashCode());
      result = prime * result + ((vendor == null) ? 0 : vendor.hashCode());
      return result;
  }
  @Override
  public boolean equals(Object obj) {
      if (this == obj)
          return true;
      if (obj == null)
          return false;
      if (!(obj instanceof VendorStudentTestPK))
          return false;
      VendorStudentTestPK other = (VendorStudentTestPK) obj;
      if (student == null) {
          if (other.student != null)
              return false;
      } else if (!student.equals(other.student))
          return false;
      if (test == null) {
          if (other.test != null)
              return false;
      } else if (!test.equals(other.test))
          return false;
      if (vendor == null) {
          if (other.vendor != null)
              return false;
      } else if (!vendor.equals(other.vendor))
          return false;
      return true;
  }

}

Hibernate mapping files as follows

vendor.hbm.xml


  </class>

vendor_student_test.hbm.xml

<class name="Vendor" table="vendor">
  <composite-id name="vendorStudentTestPK"
        class="com.onlineexam.model.VendorStudentTestPK">
        <key-property name="vendorId" column="vendor_id"
            type="java.lang.Long" />
        <key-property name="studentId" column="student_id"
            type="java.lang.Long" />
        <key-property name="testId" column="test_id" type="java.lang.Long" />
    </composite-id>

  <many-to-one name="vendor" class="Vendor" insert="false" update="false"/>
    <many-to-one name="student" class="Student" insert="false" update="false"/>
    <many-to-one name="test" class="Test" insert="false" update="false"/>

  </class>

student.hbm.xml

<class name="Student" table="student">
  <id name="studenId" type="long" column="student_id">
      <generator class="increment"/>      
      </id>
      <property name="studentName" column="student_name" type="string"/>
      <property name="studentPassword" column="student_password" type="string"/>
      <set name="tests" table="vendor_student_test"
                inverse="true" lazy="true" fetch="select">
            <key>
                <column name="student_id" not-null="true" />
            </key>
            <one-to-many class="VendorStudentTest" />
        </set>

  </class>

test.hbm.xml

//this is mapped to other table 

I am new to hibernate, Is this correct mapping?

share|improve this question
I think you will find the solution in this link mkyong.com/hibernate/… – user15870 Aug 20 '12 at 6:18
thaks alot Anil Kumar, but have to do with hbm mappings, can u tell me above mapping is correct or not? – Ramesh K Aug 20 '12 at 10:38

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.