0

I am trying to develop a web application and I was wondering if there is a way to utilize a foreign key without writing a lot of code.

My Trainees.java

@Entity
public class Trainees {

    @Id
    @GeneratedValue
    private int traineesID;
    private int groupsID;
    @ManyToOne
    @JoinColumn(name = "status_trainee")
    private String status_TraineeID;
    private int customersID;
    private String name;
    private String surname;
    private String phoneDetails;
    private String email;

    public Trainees(){

    }

    public Trainees(String name, String surname, String phoneDetails, String email, int id, int groupsID, String status_TraineeID, int customersID) {
        super();
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.phoneDetails = phoneDetails;
        this.groupsID = groupsID;
        this.status_TraineeID = status_TraineeID;
        this.customersID = customersID;
    }

    //getters and setters

    @Override
    public boolean equals(Object object) {
        if (object instanceof Trainees){
            Trainees contact = (Trainees) object;
            return contact.traineesID == traineesID;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return traineesID;
    }
}

Status_Trainee.java

@Entity
public class Status_Trainee {

    @Id
    @GeneratedValue
    private int status_traineeID;
    private String value;

    public Status_Trainee(){

    }

    public Status_Trainee(String value, int id) {
        super();
        this.value = value;
    }

    //getters and setters

    @Override
    public boolean equals(Object object) {
        if (object instanceof Status_Trainee){
            Status_Trainee value = (Status_Trainee) object;
            return value.status_traineeID == status_traineeID;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return status_traineeID;
    }
}

Error: Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on uaiContacts.model.Trainees.status_TraineeID references an unknown entity: String

So my aim is that using the Trainees table and class, I could retrieve the value of Status_Trainee table using the foreign key. For example: if foreign keys ID is 2, then it would retrieve a string from status_trainee table where primary key would match the foreign key ID.

I am using models, controlers, hibernate and angularjs to display to the view, I don't really want to pass the table through all this, I thought using something like ManyToOne or JoinColumns would retrieve the value?

Thanks for all the help!

Spinxas
  • 71
  • 1
  • 2
  • 15
  • possible duplicate of [How can I create a foreign key constraint using hibernate annotations?](http://stackoverflow.com/questions/15426736/how-can-i-create-a-foreign-key-constraint-using-hibernate-annotations) – Himanshu Tyagi Oct 01 '14 at 15:16
  • https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html – Himanshu Tyagi Oct 01 '14 at 15:16
  • @NoComments Hello thanks for the information, I have updated the question to reflect more into my problem. I was wondering is it the structure problem? – Spinxas Oct 01 '14 at 15:29

2 Answers2

1

You should add a reference to StatusTrainee in Trainee and annotate that with OneToMany, ManyToOne or OneToOne. Depending on which kind of relationship you will need a list of StatusTrainee or just a StatusTrainee.

Tip: dont use underscores in class names.

Juru
  • 1,623
  • 17
  • 43
  • Hello, thanks for reply, I included @ ManyToOne @ JoinColumn(name = "status_trainee") and instead of int I implemented string, but the problem is that is does not recognize the entity as String or Int, is there a need for different structure? – Spinxas Oct 01 '14 at 15:27
  • You shouldn't use the field of the id as reference but the entire object. Hibernate will know to use the foreign key. So use private Status_Trainee statusTrainee; as variable and annotate that. Annotating primitive types like int makes no sense. – Juru Oct 01 '14 at 15:29
1

First of all, it is not recommended to use "_" in a class name when using hibernate. Hibernate uses underscores when accessing foreignKeys. So Lets Say you rename your class to: TraineeStatus and the id change it to traineeStatusId..

Secondly, You can use the Hibernate annotations for what you need. but first you need to know how the relation is:

@OneToMany : One Trainee can have lots of statuses

@ManytoOne : Many trainees can have the same status

@OneToOne : one Trainee Can only have one status and the other way around.

Try this:

@Entity public class Trainees {

@Id
@GeneratedValue
private int traineesID;
private int groupsID;

@OneToOne
private TraineeStatus status;
private int customersID;
private String name;
private String surname;
private String phoneDetails;
private String email;

...

You can change the @OneToOne for the one you need..

Remember that hibernate will try to map this in your Trainees mysql table as status_traineeStatusId, so if you have this column (as an integer) at your trainess table you are done :)..

That is it.. Hope it helps

Cesar Villasana
  • 681
  • 3
  • 6