Zdravím vespolek,
bych měl jednu otázku k vazbě many to many. Pročetl jsem všechy možný tutoriály a stále mi uniká nějaká souvislost. proč se mi neuloží hodnoty do relační tabulky.
výchozí situace, mam uzivatele v db
chci vytvořit roli a přiřadit mu jí.
roli vytvořím, uloží se do tabulky rolí, ale ne do vazební.
Používám
Spring Tool (eclipse)
- spring framework 2.5.6
- eclipselink jpa 1.1.2
- databazi oracle 10.2
DB
User <----> Role
user má více rolí, role může být u vice userů
tzn: users ---- user_role ---- roles
Konkrétní příklad kodu:
vynechal jsem spoustu omáčky okolo jen to co jsem považoval za důležité.
Díky za radu
:)
//=================== entitní kousky kodu
public class User implements Serializable {
...
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(
name="USER_ROLE",
joinColumns={ @JoinColumn(name="USER_ID") },
inverseJoinColumns={ @JoinColumn(name="ROLE_ID") }
)
private Collection<Role> roles;
...
}
public class Role implements Serializable {
...
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(
name="USER_ROLE",
joinColumns={ @JoinColumn(name="ROLE_ID") },
inverseJoinColumns={ @JoinColumn(name="USER_ID") }
)
private Collection<User> users;
...
}
//======== pomocné kousky kodu z třídy UserService
public User createUser(String nick, String email) {
User user = new User();
user.setEmail(email);
user.setNick(nick);
em.persist(user);
return user;
}
public Role createRole(String name, String description) {
Role role = new Role();
role.setName(name);
role.setDescription(description);
em.persist(role);
return role;
}
public User addUserRole(long userId, long roleId) {
User user = em.find(User.class, userId);
Role role = em.find(Role.class, roleId);
ArrayList<Role> r = new ArrayList<Role>();
r.add(role);
user.setRoles(r);
//role.addUser(user);
return user;
}
// =========== metoda main
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
emf = (EntityManagerFactory) ctx.getBean("entityManagerFactory");
em = emf.createEntityManager();
UserService us = new UserService(em);
manyToManyExample();
em.close();
emf.close()
}
private static void manyToManyExample() {
// vrati usera co uz existuje v tabulce
User user = getUser(1);
Role role = createRole("nova", "testovaci");
// role se opravdu vytvori
System.out.println("USER ID: " + user.getId() + "ROLE ID: " + role.getId());
getUserService().addUserRole(user.getId(), role.getId());
}
Fórum › Java
JPA, @ManyToMany nevkládá vazbu

pokud to neni nejaky superprogram tak se o vazby musis starat ty... nic to za tebe neudela.. databaze maximalne zkontroluje integritu (tj jestli nevkladas reference na neco co neexistuje) (samozrejme musis mit nastaveny cizi klice)
To KIIV : ja jsem pravě měl pocit že vkládání do relačni tabulky řeší jednotlivé anotace... viz @ManyToMany(cascade=CascadeType.ALL)
@JoinTable(
name="USER_ROLE",
joinColumns={ @JoinColumn(name="ROLE_ID") },
inverseJoinColumns={ @JoinColumn(name="USER_ID") }
)
to je chybný předpoklad?

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 39 hostů
Podobná vlákna
Nevkládá zápis do DB — založil kozusnik
Získejte zpětnou vazbu pro svůj web na www.KritikaWebu.cz — založil KritikaWebuCZ
JPA 2.0 realizace relace — založil Martin
JPA + vlákna + transakce v javě SE — založil Arnold Judas Rimmer
Jak propojit DB tabulky ve Spring JPA — založil ingiraxo
Moderátoři diskuze