vendredi 23 janvier 2015

is this code normal behavior of ORMs?

I have these two objects : Project and History


Here are the minimal codes of those classes


Project



@Entity(name = "projects")
public class Project {

@Id
private String name;

@OneToMany(MappedBy = "project")
private List<History> histories;
}


History



@Entity(name = "histories")
public class History {

@Id
private Integer id;

@ManyToOne
@JoinColum(name = "project_name")
private Project project;
}


I have no troubles creating the EntityManager and creating the tables. Everything is here with the right columns gently waiting for the data to be inserted.


But then, I am not sure if this is the right behavior of ORMs.


Basically it would be ideal if it could look like that :



  • create History + set History

  • create Project + set Project + add History in Project's list histories

  • insert Project in the database using its DAO


When I try to execute such a code, only the projects table has data in, histories table is empty, which force me to write my code as folow :



  • create History + set History

  • create Project + set Project + add History in Project's list histories

  • insert History in the database using its DAO

  • insert Project in the database using its DAO


Ok, this is just one extra line of code but what if I need to fetch the data or add more histories to one Project ? If so, I would need to load the project and all the associated histories and add the physical histories to the Project object... such a code would easily get cumbersome and bothersome to write.


Anyways, is this the normal behavior of any ORMs or is my ORM fooling me ?


note : I'm using ORMLite with SQLite driver.


Aucun commentaire:

Enregistrer un commentaire