HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Many-to-many hibernate mapping if link table is having extra columns mappings

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
columnshavingmanyhibernatemappingmappingstablelinkextra

Problem

I have many-to-many mapping with extra columns in the join table. The table structure looks 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


The link:

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


I've created POJO classes as follows:

Vendor.java

public class Vendor {
    private Long vendorId;
    private String vendorName;
    private String vendorPassword;
    private Set tests;
    private boolean isVendorActivate;
    public boolean isVendorActivate() {
        return isVendorActivate;
    }
    public void setVendorActivate(boolean isVendorActivate) {
        this.isVendorActivate = isVendorActivate;
    }
    public Set getTests() {
        return tests;
    }
    public void setTests(Set 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;
    }

}


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) {

Solution

I'm not familiar with Hibernate mappings, so just some other notes about the code:

-
Calling a class Test could be a disadvantage later. Build frameworks/tools could recognize your class erroneously as a JUnit test class. Although this might be the proper term in your domain I would still consider using a synonym.

-
It's confusing that some fields are at the end of the class, like these:

private int testLevel;
private Set testQuestions;


(Java Coding Conventions, 3.1.3 Class and Interface Declarations)

-
Instead of

boolean isVendorActivate;


I'd consider an enum. Enums are easier to read (you don't have to remember what true or false means) and later you might have other states which only need a new enum value (it's easier to extend).

-
double result;


Are you sure that you want to use floating point values here? Floating point numbers are not precise, consider using a BigDecimal.

  • Why not use Double or Float to represent currency?



  • Effective Java, 2nd Edition, Item 48: Avoid float and double if exact answers are required



-
About the comment:

public VendorStudentTestPK() {
    super();
    // TODO Auto-generated constructor stub
}


If you have something to do then do it, otherwise remove the comment. It's just noise. (Clean Code by Robert C. Martin: Chapter 4: Comments, Noise Comments)

-
VendorStudentTestPK implements hashCode and equals and they call Student's, Test's and Vendor's hashCode/equals but none of these classes implements hashCode nor equals. Are you sure that they shouldn't have hashCode and equals?

Code Snippets

private int testLevel;
private Set<Question> testQuestions;
boolean isVendorActivate;
double result;
public VendorStudentTestPK() {
    super();
    // TODO Auto-generated constructor stub
}

Context

StackExchange Code Review Q#11474, answer score: 5

Revisions (0)

No revisions yet.