[jboss-user] [EJB 3.0] - JBoss creates wrong SQL query

heinrich do-not-reply at jboss.com
Fri Jul 14 03:55:04 EDT 2006


Hi,
im running into some trouble using jboss_4.0.4GA. I'm using the EntityManager's method "find" to get an entity filled with data from a prefilled database (Postgres 7.4). The problem is, that the nested associations does not contain the right data that is in database. There is some data, but not the one that should be there.
 
I have the following Entities:
The Entity MainDrug contains a Set of AdditionalSubstances.


  | @javax.persistence.Entity
  | @javax.persistence.Table(name = "main_drugs", schema="prescription")
  | public class MainDrug implements Serializable {
  | 
  | 	private java.lang.String drugId;
  | 
  | 	@javax.persistence.Column(name = "drugid")
  | 	public java.lang.String getDrugId() {
  | 		return drugId;
  | 	}
  | 
  | 	/**
  | 	 */
  | 	public void setDrugId(java.lang.String dugId) {
  | 		this.drugId = dugId;
  | 	}
  | 
  |           /** Association. */
  | 	private java.util.Set<AdditionalSubstance> additionalSubstances;
  | 
  | 	@javax.persistence.OneToMany(cascade = javax.persistence.CascadeType.ALL, fetch = javax.persistence.FetchType.EAGER)
  | 	@javax.persistence.JoinColumn(name = "id")
  | 	public java.util.Set<AdditionalSubstance> getAdditionalSubstances() {
  | 		return additionalSubstances;
  | 	}
  | 
  | 	/**
  | 	 */
  | 	public void setAdditionalSubstances(java.util.Set<AdditionalSubstance> additionalSubstances) {
  | 		this.additionalSubstances = additionalSubstances;
  | 	}
  | }

The Entity AdditionalSubstances is made of a composed PrimaryKey containing an id and two foreign keys.
Each AdditionalSubstance has an attached Substance object.

  | @javax.persistence.Entity
  | @javax.persistence.Table(name = "additional_substances", schema="prescription")
  | public class AdditionalSubstance implements Serializable {
  | 
  | 
  |     /** Primary key. */
  | 	private AdditionalSubstancePK primaryKey;
  | 
  | 
  | 	public AdditionalSubstance() {
  | 		primaryKey = new AdditionalSubstancePK();
  | 	}
  | 
  | 	@javax.persistence.EmbeddedId
  | 	public AdditionalSubstancePK getPrimaryKey() {
  | 		return primaryKey;
  | 	}
  | 
  | 	/**
  | 	 */
  | 	public void setPrimaryKey(AdditionalSubstancePK primaryKey) {
  | 		this.primaryKey = primaryKey;
  | 	}
  | 
  | 	
  | 
  | 	/** Association. */
  | 	private Substance substance;
  | 
  | 	/**
  | 	 * Get the substance.
  | 	 *
  | 	 * @return The substance.
  | 	 */
  | 	@javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.EAGER)
  | 	@javax.persistence.JoinColumn(name = "substances_id", insertable = false, updatable = false)
  | 	public Substance getSubstance() {
  | 		return substance;
  | 	}
  | 
  | 	/**
  | 	 * Set the substance.
  | 	 *
  | 	 * @param substance The substance.
  | 	 */
  | 	public void setSubstance(Substance substance) {
  | 		this.substance = substance;
  | 	}
  | 
  | 	/**
  | 	 * Primary key class for additional_substances.
  | 	 *
  | 	 */
  | 	@javax.persistence.Embeddable
  | 	public static class AdditionalSubstancePK implements Serializable {
  | 
  | 		/**
  |          * 
  |          */
  |         private static final long serialVersionUID = 1L;
  |         /** PK field. */
  | 		private java.lang.Integer id;
  | 
  | 		/**
  | 		 * Get the id.
  | 		 *
  | 		 * @return The id.
  | 		 */
  |        
  |         @Id
  |         @GeneratedValue(strategy=GenerationType.IDENTITY)
  | 		@Column(name = "id")
  | 		public Integer getId() {
  | 			return id;
  | 		}
  | 
  | 		/**
  | 		 * Set the id.
  | 		 *
  | 		 * @param id The id.
  | 		 */
  | 		public void setId(Integer id) {
  | 			this.id = id;
  | 		}
  | 
  |         
  | 		/** PK field. */
  | 		private java.lang.Integer mainDrugId;
  | 
  | 		/**
  | 		 * Get the mainDrugId.
  | 		 *
  | 		 * @return The mainDrugId.
  | 		 */
  | 		@javax.persistence.Column(name = "main_drugs_id")
  | 		public java.lang.Integer getMainDrugId() {
  | 			return mainDrugId;
  | 		}
  | 
  | 		/**
  | 		 * Set the mainDrugId.
  | 		 *
  | 		 * @param mainDrugId The mainDrugId.
  | 		 */
  | 		public void setMainDrugId(java.lang.Integer maindrugId) {
  | 			this.mainDrugId = maindrugId;
  | 		}
  | 
  | 		/** PK field. */
  | 		private java.lang.Integer substancesId;
  | 
  | 		/**
  | 		 * Get the substancesId.
  | 		 *
  | 		 * @return The substancesId.
  | 		 */
  | 		@javax.persistence.Column(name = "substances_id")
  | 		public java.lang.Integer getSubstancesId() {
  | 			return substancesId;
  | 		}
  | 
  | 		/**
  | 		 * Set the substancesId.
  | 		 *
  | 		 * @param substancesId The substancesId.
  | 		 */
  | 		public void setSubstancesId(java.lang.Integer substancesId) {
  | 			this.substancesId = substancesId;
  | 		}
  | 	}
  | }

And finaly the Substance Entity:


  | @javax.persistence.Entity
  | @javax.persistence.Table(name = "substances", schema="prescription")
  | public class Substance extends GECAMedEntityBean implements Serializable {
  | 
  | 	/**
  |      * 
  |      */
  |     private static final long serialVersionUID = 1L;
  |    
  |     private Integer id;
  | 
  |     @Id
  |     @GeneratedValue(strategy=GenerationType.IDENTITY)
  |     @Column(name = "id")
  |     public Integer getId() 
  | 	{
  | 	   return this.id;
  | 	}
  | 
  |     public void setId (Integer id) 
  | 	{
  | 	   this.id = id;
  | 	}
  |         
  | 	/** Regular field. */
  | 	private java.lang.String name;
  | 
  | 	/**
  | 	 * name of the substance
  | 	 *
  | 	 * Get the name.
  | 	 *
  | 	 * @return The name.
  | 	 */
  | 	@javax.persistence.Column(name = "name")
  | 	public java.lang.String getName() {
  | 		return name;
  | 	}
  | 
  | 	/**
  | 	 * Set the name.
  | 	 *
  | 	 * @param name The name.
  | 	 */
  | 	public void setName(java.lang.String name) {
  | 		this.name = name;
  | 	}
  | }
  | 



The code in the SessionBean looks like this:

  | MainDrug drug = em.find(MainDrug.class, id);
  | 

The MainDrug object is correct. But the nested Substances are not the ones that are linked in the database. They are just some nearly "random" Substances. Always the same for a MainDrug, but not the right one.

Is there something wrong in my AdditionalSubstances? I checked every part of the application. The EntityManager gets the correct id to search for.

Anyone an idea?

Thanks

 Martin



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

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



More information about the jboss-user mailing list