hello,
I have 3 tables in my database:
ROLE (ID_ROLE, DESCRIPTION)
ROLE_PERSON (ID_ROLE, ID_PERSON)
PERSON (ID_PERSON, NAME)
and mapped them as a ManyToMany-Relation:
Code:
@Entity
public class Person implements Serializable {
private int idPerson;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID_PERSON", nullable = false, insertable = true, updatable = true)
public int getIdPerson() {
return idPerson;
}
public void setIdPerson(int idPerson) {
this.idPerson = idPerson;
}
...
private Set<Role> roles = new HashSet<Role>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="ROLE_PERSON",
joinColumns= {@JoinColumn (name="ID_PERSON", referencedColumnName="ID_PERSON")},
inverseJoinColumns= {@JoinColumn (name="ID_ROLE", referencedColumnName="ID_ROLE") } )
public Set <Role> getRoles() {
return roles;
}
public void setRoles(Set <Roles> roles) {
this.roles= roles;
}
..
Code:
@Entity
public class Role {
private Integer idRoles;
@Id
@Column(name = "ID_ROLE", nullable = false)
public Integer getIdRole() {
return idRole;
}
public void setIdRoles(Integer idRole) {
this.idRole= idRole;
}
private Set<Person> persons= new HashSet<Person>();
@ManyToMany(cascade = CascadeType.ALL, mappedBy="roles")
public Set<Person> getPerson() {
return persons;
}
public void setPerson(Set<Person> persons) {
this.persons= persons;
}
The relationship works well by doing some HQLs (JPA-QLs). But when I want to persist a new Person-Instance, the following stacktrace occurs:
Code:
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of entity.Role.idRole
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3591)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3307)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:218)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.EntityType.replace(EntityType.java:253)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:451)
at org.hibernate.type.CollectionType.replace(CollectionType.java:518)
at org.hibernate.type.TypeFactory.replace(TypeFactory.java:482)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:340)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:153)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:126)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:227)
... 140 more
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
... 158 more
This is my session-bean:
Code:
public void persistPerson(Person person)
{
person.setName(this.person.getName());
person.setRoles(this.person.getRoles());
entityManager.persist(person);
This is my view to obtain the list of selected roles (this works):
Code:
<s:label value=Name"/>
<h:inputText value="#{bean.name}" required="true"/>
<s:label value="Select Roles"/>
<h:selectManyCheckbox value="#{users.roles}">
<s:selectItems value="#{bean.rolesList}" var="r" label="#{r.description}" itemValue="#{r.idRole}"/>
</h:selectManyCheckbox>
The persist works only, if the user will not select any roles. Then a person-instance with its name and id will be stored in the database. By persisting any roles within a user, the stacktrace above occurs.
I have tried a lot but I cannot solve the problem.
Any Idea, how I can solve that problem??
Thanks.