0

I have following two tables with One to Many relation :

Student
-------
Integer Roll_No (pk)
Integer Class (pk)
.....


Attendance
-------
Long Id (Pk)
Integer Roll_No (fk to Student.Roll_No)
Integer Class (fk to Student.Class)
.....

How to do mapping for this two tables in hibernate.Please help.Thanks in advance.

psms
  • 851
  • 3
  • 18
  • 35
  • 1
    A similar question was already asked at [JPA / Hibernate OneToMany Mapping, using a composite PrimaryKey](http://stackoverflow.com/questions/12952141/jpa-hibernate-onetomany-mapping-using-a-composite-primarykey). Try to solve it using the provided answers. From your question it is not clear if you looking for a bidirectional solution and what's the type of `Roll_No` and `Class` are they also represented as Objects and whats the relationship. Hibernate or JPA Mapping? – andih Jun 06 '15 at 07:03
  • @andih : I have updated the question with data types.I'am looking for bidirectional solution in Hibernate Mapping. – psms Jun 06 '15 at 07:36
  • You've answered only parts of my questions. Open Questions are still "Hibernate or JPA Mapping?" and "Whether Roll_No is only an Integer or also a foreign key"? Did you have a look at the link I've provided you? Have you tried to solve the problem with the help of the answers? Where are stuck? Maybe you should read the [Asking about Homework](http://meta.stackexchange.com/a/10812) section. – andih Jun 06 '15 at 07:58

1 Answers1

0
You can create a composite id class that only have the Student:

@Embeddable
public class Attendance implements java.io.Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
    @JoinColumn(name = "Roll_no", updatable = true)
    private Student Roll_no;

    @OneToMany(cascade = {}, fetch = FetchType.LAZY)
    @JoinColumn(name = "Class", updatable = true)
    Private Student Class;
    // ...
}

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Student{

    @Id
    public Roll_no  getRoll_no()
    public Class getClass()
    // ...
}
Vignesh Shiv
  • 1,129
  • 7
  • 22
  • 1
    Your Student does not have a composite primary key. It's unclear why Attendance is marked as Embeddable and why your Student class is abstract. – andih Jun 06 '15 at 07:11