persistence@glassfish.java.net

Re: select after insert issue

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Fri, 05 May 2006 10:51:12 -0700

Please make sure that you set relationship on both sides
a.setB(b);
b.setA(a);
Or similar for the collections.
Entities do not have managed relationships as oppose to CMP. And even
though some providers will populate the other side on reload, there is
no requirement to do so in the spec.

Regards,
-marina

Davy De Durpel wrote:
> I have modified the code and now the Reservation Entity Bean and all
> other dependent Enity Benas are instanciated somewhere in the Session
> Bean but the problem still appears. I also tried by first merging the
> User and Asset and then persist the Reservation but that didn't help:
>
> ...
> reservation.setUser(em.merge(reservation.getUser()));
> reservation.setAsset(em.merge(reservation.getAsset()));
> em.persist(reservation);
> ...
>
> The fact is that as soon as I persist a new Reservation (also in the
> case of a new Asset), all related objects (User and Asset in this case)
> are always returned as empty objects from the EntityManager. It seems
> like the EntityManager creates the objects but lacks to set the values
> on the fields.
>
> I will create a simple test case as soon as I find a spare minute of
> time. Is it ok that I put everything in an ear (source code included)
> and that I send it to this group? Can I send it as an attachment or do
> I have to upload it somewhere?
>
> Thanx for the support so far.
>
> On May 05, 2006 12:00 PM, Davy De Durpel <davy.de.durpel_at_isencia.be>
> wrote:
>
>
>>Sorry for the late response but yesterday I was all day travelling to
>>Ireland.
>>
>>I haven't got the time though to finish the test case and it might be
>>that I don't have the time until I get home by the end of next week to
>>do it (due to those fantastic pubs in Galway) but I did however see
>>something that might be the cause of my problem.
>>
>>When I want to 'create' a new Reservation, I just make an instance of
>>the Reservation Entity at the servlet level whereas for a 'select' or
>>an
>>'edit 'and get the Entity from the Stateless Session Bean. The
>>servlet
>>container is a 2.3 version (and not the newer 2.4) because it is
>>required for Wicket.
>>This probably means that the Entity bean might not be correctly
>>initialized because of this.
>>Or does this not make a difference?
>>
>>Anyway, if I find the time today, I will modify my code so that all
>>instances of the Entity Beans are created in the Session Beans. It
>>seems to be a better way for working with Entities.
>>
>>
>>On May 03, 2006 06:36 PM, Craig L Russell <Craig.Russell_at_Sun.COM>
>>wrote:
>>
>>
>>>Hi Davy,
>>>
>>>>>>>The problem in short is that when I do a persist of a new
>>>>>>>entity
>>>>>>>and
>>>>>>>then I do a select to retrieve all entities from the database,
>>>>>>>then all
>>>>>>>referenced objects are empty (all properties are Null). No way
>>>>>>>to get
>>>>>>>them back then restarting the application. In all other
>>>>>>>situations
>>>>>>>(selects, updates) I don't have the problem but things go wrong
>>>>>>>as soon
>>>>>>>as I persist a new Entity to the database.
>>>
>>>The code you attached here is quite useful. Now, could you please
>>>send a test case that shows the problem you're having? I could
>>>imagine how you're using the stateless bean but it would be easier
>>>if
>>>you could send along the client code that shows the symptoms.
>>>
>>>Thanks,
>>>
>>>Craig
>>>
>>>On May 3, 2006, at 2:14 AM, Davy De Durpel wrote:
>>>
>>>
>>>>Hi,
>>>>
>>>>Some nice lady form the users list adviced me to also send my
>>>>problem to the persistence list, and here it is...
>>>>
>>>>Byeee...
>>>>
>>>>>Hi,
>>>>>
>>>>>Ok, that are a lot of logical questions from several mails but I
>>>>>don't think I can provide an answer to all of them because I'm
>>>>>rather new to the stuff. The thing is that I tried to use as much
>>>>>as possible the default values because I like to develop by
>>>>>exception.
>>>>>
>>>>>Something about my development environment:
>>>>>
>>>>>- JDK 1.5
>>>>>- Glassfish B42 and B46
>>>>>- JavaDB (the one that comes with Glassfish)
>>>>>- Eclipse 3.2 M6
>>>>>- WTP 1.5 M6
>>>>>- Glassfish plugin 0.2.1
>>>>>- Wicket 1.2 RC2
>>>>>
>>>>>What follows are the cleaned up Entity pojo's (I removed
>>>>>comments,
>>>>>imports and setters) , the code I use to persist and to do the
>>>>>select and the data model. At the end there is some more
>>>>>information about the application and some answers to the
>>>>>questions.
>>>>>
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>--------
>>>>>@Entity
>>>>>@IdClass(ReservationPK.class)
>>>>>@Table(name="RESERVATIONS")
>>>>>public class Reservation implements ReservationRemote {
>>>>>
>>>>>private static final long serialVersionUID =
>>>>>7731097732230332469L;
>>>>>
>>>>>private Integer ID;
>>>>>private Asset asset;
>>>>>private Integer assetID;
>>>>>private User user;
>>>>>private Date dateFrom;
>>>>>private Date dateTo;
>>>>>private User CreatedBy;
>>>>>private Date creationDate;
>>>>>
>>>>>@ManyToOne
>>>>>@JoinColumn(name = "ASSET_ID", referencedColumnName="ID")
>>>>>public Asset getAsset() {
>>>>>return asset;
>>>>>}
>>>>>@ManyToOne
>>>>>@JoinColumn(name="CREATED_BY", referencedColumnName="ID")
>>>>>public User getCreatedBy() {
>>>>>return CreatedBy;
>>>>>}
>>>>>@Column(name="CREATION_DATE")
>>>>>@Temporal(TemporalType.DATE)
>>>>>public Date getCreationDate() {
>>>>>return creationDate;
>>>>>}
>>>>>@Column(name="DATE_FROM")
>>>>>@Temporal(TemporalType.DATE)
>>>>>public Date getDateFrom() {
>>>>>return dateFrom;
>>>>>}
>>>>>@Column(name="DATE_TO")
>>>>>@Temporal(TemporalType.DATE)
>>>>>public Date getDateTo() {
>>>>>return dateTo;
>>>>>}
>>>>>@Id
>>>>>@Column(name="ID")
>>>>>public Integer getID() {
>>>>>return ID;
>>>>>}
>>>>>@Id
>>>>>@Column(name="ASSET_ID", nullable=false, updatable=false,
>>>>>insertable=false)
>>>>>public Integer getAssetID() {
>>>>>return assetID;
>>>>>}
>>>>>@ManyToOne(targetEntity=User.class)
>>>>>@JoinColumn(name="USER_ID", referencedColumnName="ID")
>>>>>public User getUser() {
>>>>>return user;
>>>>>}
>>>>>}
>>>>>
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>--------
>>>>>@Entity
>>>>>@Table(name="ASSETS")
>>>>>public class Asset implements AssetRemote {
>>>>>private static final long serialVersionUID =
>>>>>7293500542981293826L;
>>>>>
>>>>>private Integer ID;
>>>>>private AssetType assetType;
>>>>>private String location;
>>>>>private User owner;
>>>>>private Integer minReservations;
>>>>>private Integer maxReservations;
>>>>>private User createdBy;
>>>>>private Date creationDate;
>>>>>
>>>>>@ManyToOne
>>>>>@JoinColumn(name="ASSET_TYPE_ID", referencedColumnName="ID")
>>>>>public AssetType getAssetType() {
>>>>>return assetType;
>>>>>}
>>>>>@ManyToOne
>>>>>@JoinColumn(name="CREATED_BY", referencedColumnName="ID")
>>>>>public User getCreatedBy() {
>>>>>return createdBy;
>>>>>}
>>>>>@Column(name="CREATION_DATE")
>>>>>@Temporal(TemporalType.DATE)
>>>>>public Date getCreationDate() {
>>>>>return creationDate;
>>>>>}
>>>>>@Id
>>>>>@GeneratedValue(strategy=GenerationType.IDENTITY)
>>>>>public Integer getID() {
>>>>>return ID;
>>>>>}
>>>>>@Column(name="LOCATION")
>>>>>public String getLocation() {
>>>>>return location;
>>>>>}
>>>>>@Column(name="MAX_RESERVATION")
>>>>>public Integer getMaxReservations() {
>>>>>return maxReservations;
>>>>>}
>>>>>@Column(name="MIN_RESERVATION")
>>>>>public Integer getMinReservations() {
>>>>>return minReservations;
>>>>>}
>>>>>@ManyToOne
>>>>>@JoinColumn(name="OWNER_ID", referencedColumnName="ID")
>>>>>public User getOwner() {
>>>>>return owner;
>>>>>}
>>>>>}
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>--------
>>>>>@Stateless
>>>>>public class Reservations implements Serializable,
>>>>>ReservationsRemote {
>>>>>
>>>>>private static final long serialVersionUID =
>>>>>1062786124637158763L;
>>>>>
>>>>>@PersistenceContext(unitName="AssMaEM")
>>>>>EntityManager em;
>>>>>
>>>>>@SuppressWarnings("unchecked")
>>>>>public List<Reservation> findAllForUser(int first, int count,
>>>>>String sortProperty, boolean sortAsc, User user) {
>>>>>
>>>>>String ejbql = "SELECT c FROM Reservation c WHERE c.user
>>>>>= :user";
>>>>>Query query = em.createQuery(ejbql);
>>>>>query.setParameter("user", user);
>>>>>
>>>>>return query.getResultList();
>>>>>}
>>>>>
>>>>>public void update(Reservation reservation) {
>>>>>Reservation mergedReservation = em.merge(reservation);
>>>>>em.persist(mergedReservation);
>>>>>}
>>>>>
>>>>>public ReservationRemote create(Reservation reservation) {
>>>>>em.persist(reservation);
>>>>>return reservation;
>>>>>}
>>>>>}
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>--------
>>>>>CREATE SCHEMA ASSMA;
>>>>>
>>>>>CREATE TABLE USERS
>>>>>(
>>>>>ID INTEGER PRIMARY KEY not null GENERATED ALWAYS AS IDENTITY,
>>>>>USER_NAME CHAR(8) not null,
>>>>>FULL_NAME VARCHAR(50) not null,
>>>>>PASSWORD VARCHAR(30) not null,
>>>>>EMAIL_ADDRESS VARCHAR(80)
>>>>>);
>>>>>
>>>>>CREATE TABLE USERS_ROLES
>>>>>(
>>>>>USER_ID INTEGER not null,
>>>>>ROLE_ID INTEGER not null,
>>>>>CONSTRAINT USERS_ROLES_PK PRIMARY KEY (USER_ID,ROLE_ID)
>>>>>);
>>>>>
>>>>>CREATE TABLE ROLES
>>>>>(
>>>>>ID INTEGER PRIMARY KEY not null GENERATED ALWAYS AS IDENTITY,
>>>>>NAME VARCHAR(50) not null
>>>>>);
>>>>>
>>>>>CREATE TABLE RESERVATIONS
>>>>>(
>>>>>ID INTEGER not null GENERATED BY DEFAULT AS IDENTITY,
>>>>>ASSET_ID INTEGER,
>>>>>USER_ID INTEGER not null,
>>>>>DATE_FROM DATE,
>>>>>DATE_TO DATE,
>>>>>CREATED_BY INTEGER not null,
>>>>>CREATION_DATE DATE not null,
>>>>>CONSTRAINT RESERVATIONS_PK PRIMARY KEY (ID,ASSET_ID)
>>>>>);
>>>>>
>>>>>CREATE TABLE ASSETS
>>>>>(
>>>>>ID INTEGER PRIMARY KEY not null GENERATED ALWAYS AS IDENTITY,
>>>>>ASSET_TYPE_ID INTEGER not null,
>>>>>LOCATION VARCHAR(50),
>>>>>OWNER_ID INTEGER not null,
>>>>>MIN_RESERVATION INTEGER,
>>>>>MAX_RESERVATION INTEGER,
>>>>>CREATED_BY INTEGER not null,
>>>>>CREATION_DATE DATE not null
>>>>>);
>>>>>
>>>>>CREATE TABLE ASSET_TYPES
>>>>>(
>>>>>ID INTEGER PRIMARY KEY not null GENERATED ALWAYS AS IDENTITY,
>>>>>NAME VARCHAR(20) not null,
>>>>>DESCRIPTION VARCHAR(50) not null
>>>>>);
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>--------
>>>>>
>>>>>- ReservationPK is just a bean with the 2 Integers that make up
>>>>>the ID and with getters and setters (no annotations)
>>>>>- All have remote interfaces with getters and setters for all of
>>>>>the properties
>>>>>- I didn't put any cascading restrictions in my code and in my
>>>>>database because this project is only an internal test project
>>>>>and
>>>>>I want to keep things simple
>>>>>- The data model is the complete model I use for this test
>>>>>project. It is far from being perfect and will not work in a
>>>>>production environment. One of my tests is to see how easy it is
>>>>>to write Entity beans on an existing data model.
>>>>>- Almost everything I use is still in beta phase but then I like
>>>>>to live on the edge ;-)
>>>>>- I like Glassfish allot but I love Wicket
>>>>>
>>>>>- I think that all entities are detached entities because I need
>>>>>to call em.merge before doing an update.
>>>>>- I do not know anything about cascade settings since I'm using
>>>>>the default but I know that when I do a persist of a new User
>>>>>that
>>>>>has Roles that don't exist in the database, that those roles are
>>>>>also persisted.
>>>>>- There are no foreign keys on the database and most of them have
>>>>>Identity fields
>>>>>- I did however define the necessary relationships between the
>>>>>Entity beans
>>>>>
>>>>>Now somebody has an idea what this mortal is doing wrong?
>>>>>
>>>>>Byeee...
>>>>>
>>>>>Gordon Yorke wrote:
>>>>>
>>>>>>Hello,
>>>>>>We are going to need more detail. What is the cascade
>>>>>>settings on the relationships from Reservation to User and
>>>>>>Asset? Are they Cascade PERSIST or cascade ALL? These pre-
>>>>>>existing User and Asset objects are they managed entities or
>>>>>>detached entities? Can you provide the code that is performing
>>>>>>this operation? Are there unique constraints on the database
>>>>>>tables?
>>>>>>--Gordon
>>>>>>
>>>>>>-----Original Message-----
>>>>>>From: Marina.Vatkina_at_Sun.COM [mailto:Marina.Vatkina_at_Sun.COM]On
>>>>>>Behalf Of
>>>>>>Marina Vatkina
>>>>>>Sent: Tuesday, May 02, 2006 12:44 PM
>>>>>>To: users_at_glassfish.dev.java.net; persistence
>>>>>>Subject: Re: select after insert issue
>>>>>>
>>>>>>
>>>>>>Hi Davy,
>>>>>>
>>>>>>CC-ing persistence alias.
>>>>>>Is it a Java SE or a Java EE application? Do you navigate to the
>>>>>>related
>>>>>>objects via getters?
>>>>>>
>>>>>>thanks,
>>>>>>-marina
>>>>>>
>>>>>>Davy De Durpel wrote:
>>>>>>
>>>>>>
>>>>>>>Hi,
>>>>>>>
>>>>>>>I have a strange behavior and I have it on build 42 and 46 (the
>>>>>>>only 2 I
>>>>>>>tested).
>>>>>>>
>>>>>>>The problem in short is that when I do a persist of a new
>>>>>>>entity
>>>>>>>and
>>>>>>>then I do a select to retrieve all entities from the database,
>>>>>>>then all
>>>>>>>referenced objects are empty (all properties are Null). No way
>>>>>>>to get
>>>>>>>them back then restarting the application. In all other
>>>>>>>situations
>>>>>>>(selects, updates) I don't have the problem but things go wrong
>>>>>>>as soon
>>>>>>>as I persist a new Entity to the database.
>>>>>>>
>>>>>>>I will try to explain in more detail.
>>>>>>>
>>>>>>>I have a table 'Reservation' that contains a link to a table
>>>>>>>'Asset' and
>>>>>>>a table 'User'.
>>>>>>>I execute this query to get all the reservations for a certain
>>>>>>>user:
>>>>>>>'SELECT c FROM Reservation c WHERE c.user = :user'
>>>>>>>Everything is left by default so it should be using eager
>>>>>>>fetching. The
>>>>>>>result is correct and also the User and Asset information is
>>>>>>>retrieved
>>>>>>>from the database.
>>>>>>>I now create a new Reservation with some default values. I also
>>>>>>>add an
>>>>>>>existing Asset and User object (detached from the entity
>>>>>>>manager).
>>>>>>>I use em.persist to save the reservation in the database and I
>>>>>>>can
>>>>>>>clearly see that this is done in a correct way.
>>>>>>>I then execute the same query again to retrieve all
>>>>>>>Reservations
>>>>>>>for the
>>>>>>>current user from the database but now my 'User' and 'Asset'
>>>>>>>objects
>>>>>>>have all fields initialized to Null values.
>>>>>>>
>>>>>>>I also tried it with other Entity beans and I have the same
>>>>>>>behavior.
>>>>>>>
>>>>>>>Any idea what could be causing this behavior or do I need to
>>>>>>>provide
>>>>>>>more details?
>>>>>>>
>>>>>>>Byeee...
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>-------------------------------------------------------------------
>>>>>>>--
>>>>>>>To unsubscribe, e-mail:
>>>>>>>users-unsubscribe_at_glassfish.dev.java.net
>>>>>>>For additional commands, e-mail:
>>>>>>>users-help_at_glassfish.dev.java.net
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>--------------------------------------------------------------------
>>>>>>-
>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>>>>>For additional commands, e-mail:
>>>>>>users-help_at_glassfish.dev.java.net
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>>>>For additional commands, e-mail:
>>>>>users-help_at_glassfish.dev.java.net
>>>
>>>Craig Russell
>>>Architect, Sun Java Enterprise System
>>>http://java.sun.com/products/jdo
>>>408 276-5638 mailto:Craig.Russell_at_sun.com
>>>P.S. A good JDO? O, Gasp!
>>>
>>
>>