[EJB 3.0] - JBoss creates wrong SQL query
by heinrich
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
19 years, 9 months
[EJB 3.0] - Are transactions used by default or do i HAVE to write trans
by Markus.Wahl
>From reading the transaction section of the trailblazer on ejb 3 (http://trailblazer.demo.jboss.com/EJB3Trail/services/transaction/index.html) I get the impression that transactions are always enabled, and that I don't have to write any code or use any transaction annotations at all, and still know that transactions are used. Am I right, or am I wrong?
It is the following words that make me assume this (from the trailblazer):anonymous wrote : If a method does not have a transaction annotation, it is assigned the default transaction property REQUIRED by the EJB 3.0 container.
But it also says: anonymous wrote : In EJB 3.0, the EntityManager must run in a transactional context to ensure the database integrity.
So, does that mean, in my session bean, that I have to obtain the transaction manager from the entity manager and use it to ensure that transactions are used? Like in the following example:
@PersistenceContext
| private EntityManager em;
| ...
| EntityTransaction tx = em.getTransaction();
| tx.begin();
|
| CD cd = new CD( "The Beatles", "Rubber Soul" );
| cd.addTrack( new Track( 220, "Norwegian Wood" ) );
| cd.addTrack( new Track( 180, "Drive My Car" ) );
| em.persist( cd );
|
| cd = new CD( "Deep Purple", "Machine Head" );
| cd.addTrack( new Track( 200, "Smoke On The Water" ) );
| cd.addTrack( new Track( 480, "Lazy" ) );
| em.persist( cd ) ;
|
| tx.commit();
| em.close();
|
Or could I simply remove the 3 lines using tx as well as remove all transation annotations from the entity bean, and still be sure that transactions are used?
Another wondering about the code above; if I only created one CD (and not two as above), would it be even relevant to talk about transactions?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3957985#3957985
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3957985
19 years, 9 months