Java – JPA/Hibernate mapping to store months and days-of-months

hibernatejavajpapersistence

I have following POJOs:

class Month {
    long id;
    String description;
    List<Day> days; // always contains 29, 30 or 31 elements
}

class Day {
    byte nr; // possible values are 1-31
    String info;
}

Is there a way to store these objects into following DB structure using JPA+Hibernate:

Table MONTHS:

id;description;

Table DAYS:

id-of-month;nr-of-day;info;

Any better solution for this situation?

Best Solution

If you can't change your pojo's or table structure you are a bit screwed. If you can then a simple annotated pojo will work.

class Month {
   @Id
   private long id;
   private String description;
   @OneToMany(mappedBy="month",fetchType=Lazy)
   private List<Day> days;

}

---- Surrogate key required DB change for Days table

class Day {
    @Id
    private int id;
    private Month month;
    private byte nr; // possible values are 1-31
    private String info;
}