persistence nového objektu v Hibernate
Zdenek Tronicek
tronicek na fel.cvut.cz
Pondělí Únor 9 16:13:19 CET 2009
Dobry den,
> protected void createEntity(User entity) {
kod v metode createEntity zajisti, ze bidirectional vztahy, napr. User
<-> GameComment, jsou nastaveny spravne. Tj. pokud User odkazuje na
nejaky GameComment, tak i GameComment musi odkazovat na User. Zda je
potreba toto delat ci ne bohuzel z ukazky nelze poznat. Mozna je to
opravdu silenost.
Z.T.
--
Zdenek Tronicek
Department of Computer Science and Engineering
Prague tel: +420 2 2435 7410
http://cs.felk.cvut.cz/~tronicek
Quoting Tomáš Procházka <t.prochazka na centrum.cz>:
>
> Zkoušel jsem REST v Netbeans tak, že jsem z DB vytvořil JPA Entity a z
> nich pak vygeneroval RESTový server, Netbeans mimo jiné vytvořil
> následující kód pro ukládání nového uživatele do DB. Samotný uživatel
> nemá žádné cizí klíče, je pouze součástí mnoha dalších tabulek. Vážně
> je nutné při ukládání nové entity provádět takovou šílenost?
>
> /**
> * Persist the given entity.
> *
> * @param entity the entity to persist
> */
> protected void createEntity(User entity) {
> entity.setId(null);
> EntityManager em =
> PersistenceService.getInstance().getEntityManager();
> em.persist(entity);
> for (GameComment value : entity.getGameCommentCollection()) {
> User oldEntity = value.getUserId();
> value.setUserId(entity);
> if (oldEntity != null) {
>
> oldEntity.getGameCommentCollection().remove(entity);
> }
> }
> for (GameFavorite value :
> entity.getGameFavoriteCollection()) {
> User oldEntity = value.getUserId();
> value.setUserId(entity);
> if (oldEntity != null) {
>
> oldEntity.getGameFavoriteCollection().remove(entity);
> }
> }
> for (CreditHistory value :
> entity.getCreditHistoryCollection()) {
> User oldEntity = value.getUserId();
> value.setUserId(entity);
> if (oldEntity != null) {
>
> oldEntity.getCreditHistoryCollection().remove(entity);
> }
> }
> for (GamePurchased value :
> entity.getGamePurchasedCollection()) {
> User oldEntity = value.getUserId();
> value.setUserId(entity);
> if (oldEntity != null) {
>
> oldEntity.getGamePurchasedCollection().remove(entity);
> }
> }
> for (Game value : entity.getGameCollection()) {
> User oldEntity = value.getAuthor();
> value.setAuthor(entity);
> if (oldEntity != null) {
> oldEntity.getGameCollection().remove(entity);
> }
> }
>
>
>
>
> Datum: 21:03:16 7. února 2009
> --
> ---------------------------------------------------------------------
> Tomáš Procházka
>
>
> E-mail: t.prochazka na centrum.cz
> WWW: http://www.atomsoft.cz
> ICQ: 87147320
> ---------------------------------------------------------------------
>
>
Další informace o konferenci Konference