[jboss-user] [Beginner's Corner] - Re: ManyToMany problem

swenu do-not-reply at jboss.com
Wed Nov 4 09:48:56 EST 2009


Yesterday i moved the code from the entity to the RoleHome.
Now looks like this in RoleHome.java
package ch.emtm.session;
  | 
  | import java.util.ArrayList;
  | import java.util.List;
  | 
  | import javax.persistence.EntityManager;
  | 
  | import org.jboss.seam.annotations.Begin;
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Logger;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.web.RequestParameter;
  | import org.jboss.seam.framework.EntityHome;
  | import org.jboss.seam.log.Log;
  | 
  | import ch.emtm.entity.Function;
  | import ch.emtm.entity.Role;
  | import ch.emtm.entity.RoleFunction;
  | 
  | @Name("roleHome")
  | public class RoleHome extends EntityHome<Role>
  | {	
  |     @RequestParameter Long roleId;
  | 	
  |     @Logger 
  |     private Log log;
  |     
  |     @In (required=false)
  | 	Function function;
  |     
  | 	EntityManager em = this.getEntityManager();
  |     
  |     @Override
  |     public Object getId()
  |     {
  |         if (roleId == null)
  |         {
  |             return super.getId();
  |         }
  |         else
  |         {
  |             return roleId;
  |         }
  |     }
  | 
  |     @Override @Begin
  |     public void create() {
  |         super.create();
  |     }
  |     
  |     @Override
  |     public String persist(){
  |     	String returnVal = super.persist();
  |     	this.roleId=this.instance.getId();  	
  |     	setFunctions(this.instance.getFunctions());
  |     	return returnVal;
  |     }
  |     
  |     @Override
  |     public String update(){
  |     	String returnVal = super.update();
  |     	this.roleId=this.instance.getId();
  |     	setFunctions(this.instance.getFunctions());
  |     	return returnVal;
  |     }
  | 
  |     public List<Function> getFunctions(){
  | 		return this.instance.getFunctions();
  | 	}
  | 	
  | 	public void setFunctions(List<Function> functions){
  | 			//no function assigned
  | 			if(functions.size() == 0){
  | 				for(RoleFunction rf:this.instance.getRoleFunctions()){
  | 					removeAssignment(rf);
  | 				}
  | 				this.instance.getRoleFunctions().clear();
  | 			}else{
  | 				//there are entries within functions
  | 				if(this.instance.getRoleFunctions() == null || this.instance.getRoleFunctions().size() < 1){
  | 					//No entries within RoleFunctions, so we could add all!
  | 					for(Function f:functions){
  | 						addAssignment(f);
  | 					}
  | 				}else{
  | 					//there are already entries within RoleFunctions
  | 					compareAssignments(functions);
  | 				}
  | 			}
  | 	}
  | 
  | 	private void compareAssignments(List<Function> functions) {
  | 		log.info("received a list to compare!Assigned are {0} entries - comparing list contains {1} entries",this.instance.getRoleFunctions().size(), functions.size());
  | 		for(Function f:functions){
  | 			boolean newAssignment = true;
  | 			for(RoleFunction rf:this.instance.getRoleFunctions()){
  | 				if(rf.getFunction().getId().equals(f.getId()))
  | 					newAssignment = false;
  | 			}
  | 			if(newAssignment)
  | 				addAssignment(f);
  | 		}
  | 		List<RoleFunction> rmRf = new ArrayList<RoleFunction>();
  | 		for (int i = this.instance.getRoleFunctions().size() - 1; i >= 0; i--){
  | 			boolean unusedAssignment = true;
  | 			for(Function f:functions){
  | 				if(this.instance.getRoleFunctions().get(i).getFunction().getId().equals(f.getId()))
  | 					unusedAssignment = false;
  | 			}
  | 			if(unusedAssignment)
  | 				rmRf.add(this.instance.getRoleFunctions().get(i));
  | 		}
  | 		for(RoleFunction rf:rmRf){
  | 			removeAssignment(rf);
  | 			this.instance.getRoleFunctions().remove(rf);
  | 		}
  | 	}
  | 
  | 	private void addAssignment(Function f) {
  | 		RoleFunction rf = new RoleFunction();
  | 		rf.setFunction(f);
  | 		rf.setRole(this.instance);
  | 		this.instance.getRoleFunctions().add(rf);
  | 	}
  | 
  | 	private void removeAssignment(RoleFunction rf) {
  | 		this.em.remove(rf);
  | 	}
  | }
RoleFunction.java
package ch.emtm.entity;
  | 
  | import java.io.Serializable;
  | 
  | import javax.persistence.Column;
  | import javax.persistence.Entity;
  | import javax.persistence.GeneratedValue;
  | import javax.persistence.Id;
  | import javax.persistence.JoinColumn;
  | import javax.persistence.ManyToOne;
  | 
  | import org.jboss.seam.annotations.Name;
  | 
  | @Entity
  | @Name("rolefunction")
  | public class RoleFunction implements Serializable
  | {
  | 	/**
  | 	 * 
  | 	 */
  | 	private static final long serialVersionUID = 1L;
  | 	
  | 	@Id
  | 	@GeneratedValue
  | 	private Long id;
  | 	@Column(name="role_id")
  | 	private long iRoleId;
  | 	@Column(name="function_id")
  | 	private long iFunctionId;
  | 	@ManyToOne
  | 	@JoinColumn(name="role_id", insertable=false, updatable=false)
  | 	private Role role = new Role();
  | 	@ManyToOne
  | 	@JoinColumn(name="function_id", insertable=false, updatable=false)
  | 	private Function function = new Function();
  | 
  | 	/**
  | 	 * @return the id
  | 	 */
  | 	public Long getId() {
  | 		return this.id;
  | 	}
  | 	/**
  | 	 * @param id the id to set
  | 	 */
  | 	public void setId(Long id) {
  | 		this.id = id;
  | 	}
  | 	/**
  | 	 * @return the iRoleId
  | 	 */
  | 	@SuppressWarnings("unused")
  | 	private long getRoleId() {
  | 		return iRoleId;
  | 	}
  | 	/**
  | 	 * @param iRoleId the iRoleId to set
  | 	 */
  | 	@SuppressWarnings("unused")
  | 	private void setRoleId(int iRoleId) {
  | 		this.iRoleId = iRoleId;
  | 	}
  | 	/**
  | 	 * @return the iFunctionId
  | 	 */
  | 	@SuppressWarnings("unused")
  | 	private long getFunctionId() {
  | 		return iFunctionId;
  | 	}
  | 	/**
  | 	 * @param iFunctionId the iFunctionId to set
  | 	 */
  | 	@SuppressWarnings("unused")
  | 	private void setFunctionId(int iFunctionId) {
  | 		this.iFunctionId = iFunctionId;
  | 	}
  | 	/**
  | 	 * @return the roleId
  | 	 */
  | 	public Role getRole() {
  | 		return this.role;
  | 	}
  | 	/**
  | 	 * @param roleId the roleId to set
  | 	 */
  | 	public void setRole(Role role) {
  | 		this.role = role;
  | 		if(role != null && role.getId() != null)
  | 			 this.iRoleId = role.getId();
  | 		else
  | 			this.iRoleId = 0L;
  | 	}
  | 	/**
  | 	 * @return the functionId
  | 	 */
  | 	public Function getFunction() {
  | 		return this.function;
  | 	}
  | 	/**
  | 	 * @param functionId the functionId to set
  | 	 */
  | 	public void setFunction(Function function) {
  | 		this.function = function;
  | 		if(function != null)
  | 			this.iFunctionId = function.getId();
  | 		else
  | 			this.iFunctionId = 0L;
  | 	}
  | 
  | }
  | 

Works anything!
Except if i create a new role - the role id isn't present!

I get the following error message:

15:44:36,587 SEVERE [component] /roleList.xhtml @72,97 value="#{roleHome.functions}": Error writing 'functions' on type ch.emtm.session.RoleHome_$$_javassist_seam_3
  | java.lang.NullPointerException
  | 	at ch.emtm.session.RoleHome.addAssignment(RoleHome.java:123)
  | 	at ch.emtm.session.RoleHome.setFunctions(RoleHome.java:83)
  | 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  | 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
i dindn't investigate a lot of time to solve, i will try this evening again. 
Would be great if you already could lead to a direction - else i'm sure i'll find something ;)

Thanks in advance - Best regards
Sven

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4263961#4263961

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4263961



More information about the jboss-user mailing list