Enregistrer ou mettre à jour une entrée avec Hibernate
Salut,
J'ai une petite confusion au niveau de hibernate quand je veux enregistrer ou mettre a jour une donnée :(
La methode merge et saveor update doivent enregistrer une entree si elle n'existe pas ou bien la mettre a jour a ce que j'ai lu des documentations. Je galere depuis un moment pour la faire marcher mais ca ne marche jamais, quand une entree existe deja, ca la met pas a jours et ca remet une erreur :(
Voici le code expliqué:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| Location loc = new Location();
LocationHome locHome = new LocationHome();
loc.setName("Annemasse");
logger.info(anotherLoc.getLocationId()); // Prints NULL
logger.info(anotherLoc.getName()); // Prints "Annemasse"
logger.info(anotherLoc.getLat()); // Prints NULL
loc = locHome.insertUpdate(loc);
// a ce niveau, la base de donnee contient location Annemasse et disant que le
// location_id alloué par mysql est 1
logger.info(logger.infoanotherLoc.getLocationId()); // Prints 1
logger.info(anotherLoc.getName()); // Prints "Annemasse"
logger.info(anotherLoc.getLat()); // Prints NULL
////////////////////////////////////////////////////////////////
// une autre execution dans le meme programme ou meme un autre programme
////////////////////////////////////////////////////////////////
Location anotherLoc = new Location();
anotherLoc.setName("Annemasse");
anotherLoc.setLat("12.02482");
// a ce point la base contient deja une location avec un nom
"Annemasse"
anotherLoc = locHome.insertUpdate(anotherLoc);
// Ici je devrai avoir aucune erreur, il devrai mettre a jour la location avec le meme nom
// mais en executant insertOrUpdate (merge, saveOrUpdate)
// ca remet une erreur a chaque fois disant duplicated entry ..
logger.info(anotherLoc.getLocationId()); // Prints 1
logger.info(anotherLoc.getName()); // Prints "Annemasse"
logger.info(anotherLoc.getLat()); // Prints "12.02482" |
Je ne sais pas si Hibernate bug ou c'est un probleme chez moi :/