User.class
Code:
@ManyToMany(
cascade={CascadeType.PERSIST, CascadeType.MERGE},
mappedBy="users",
targetEntity=Service.class
)
private Set<Service> service;
public Set<Service> getService() {
return service;
}
Service.class
Code:
@ManyToMany (
targetEntity = User.class,
cascade = {CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name = "SERVICE_USERS",
joinColumns = @JoinColumn ( name="SERVICE_ID" ),
inverseJoinColumns = @JoinColumn(name = "USER_ID")
)
private Set<User> users;
public Set<User> getUsers() {
return users;
}
then
Code:
Service service = new Service();
service.setTitle("Delete folders");
service.setDescription("Desription of service");
service.setDate( new Date() );
service.setCount( 0 );
User user = new User();
user.setFio("Ivanov Sergei Ivanovich");
user.setCreateDate(new Date());
user.setUpdateDate(new Date());
Set <User> set = new HashSet <User>();
set.add(user);
service.setUsers(set);
ORMHelper.update(service); // error
but I still get error:
Code:
Hibernate: /* insert ru.mipt.rivis.hib.example.Service */ insert into Service (COUNT, DESCRIPTION, LASTCALLDATE, TITLE) values (?, ?, ?, ?)
Hibernate: /* insert collection row ru.mipt.rivis.hib.example.Service.users */ insert into SERVICE_USERS (SERVICE_ID, USER_ID) values (?, ?)
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ru.mipt.rivis.hib.example.User
What's wrong?