-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: failed to lazily initialize a collection of role..no session
PostPosted: Tue Oct 03, 2006 4:54 am 
Newbie

Joined: Tue Oct 03, 2006 4:41 am
Posts: 2
Location: Pinkafeld/Bgld./Austria
Hi!

I'm using JBoss 4.0.4-GA with the embeddable EJB3 Container.

Using the Tools available in my IDE (IntelliJ IDEA 6.0 RC3) I reverse engineered a few Tables from a Sybase Anywahre DB to Entities.

These are:
CTCouponsEntity.java

Code:
package at.tac.ra.ng.dl.entities;

import javax.persistence.*;
import java.util.List;
import java.util.Date;
import java.io.Serializable;

/**
* @author michael
* @version 0.1.0.0 02.10.2006
*/
@Entity
@Table(name = "C_T_COUPONS")
public class CTCouponsEntity implements Serializable {
  private List<CTCouponHistoryEntity> history;
  private Integer couponid;
  private String couponno;
  private Integer number;
  private Integer year;
  private Date issuedate;
  private Date expirationdate;
  private Date clearingdate;
  private Date paiddate;
  private String buyerName;
  private String text;
  private Integer amount;
  private Double value;
  private Integer origamount;
  private Double origvalue;
  private String description;
  private String description1;
  private Boolean prolonged;
  private String stornoreason;
  private Double discountrate;
  private String discountreason;
  private Integer billno;
  private String payMethodid;
  private Integer stornoUserid;
  private Boolean maison;
  private Date canceldate;
  private Date validFrom;
  private String couponComment;

  @OneToMany(mappedBy = "couponid")
  @JoinColumn(name = "couponid", referencedColumnName = "couponid", nullable = false)
  public List<CTCouponHistoryEntity> getHistory() {
    return history;
  }

  public void setHistory(List<CTCouponHistoryEntity> history) {
    this.history = history;
  }

  @Id
  @Column(name = "couponid", nullable = false, length = 10)
  public Integer getCouponid() {
    return couponid;
  }

  public void setCouponid(Integer couponid) {
    this.couponid = couponid;
  }

  @Basic
  @Column(name = "couponno", nullable = false, length = 25)
  public String getCouponno() {
    return couponno;
  }

  public void setCouponno(String couponno) {
    this.couponno = couponno;
  }

  @Basic
  @Column(name = "number", length = 10)
  public Integer getNumber() {
    return number;
  }

  public void setNumber(Integer number) {
    this.number = number;
  }

  @Basic
  @Column(name = "year", length = 10)
  public Integer getYear() {
    return year;
  }

  public void setYear(Integer year) {
    this.year = year;
  }

  @Basic
  @Column(name = "issuedate", length = 23)
  public Date getIssuedate() {
    return issuedate;
  }

  public void setIssuedate(Date issuedate) {
    this.issuedate = issuedate;
  }

  @Basic
  @Column(name = "expirationdate", length = 23)
  public Date getExpirationdate() {
    return expirationdate;
  }

  public void setExpirationdate(Date expirationdate) {
    this.expirationdate = expirationdate;
  }

  @Basic
  @Column(name = "clearingdate", length = 23)
  public Date getClearingdate() {
    return clearingdate;
  }

  public void setClearingdate(Date clearingdate) {
    this.clearingdate = clearingdate;
  }

  @Basic
  @Column(name = "paiddate", length = 23)
  public Date getPaiddate() {
    return paiddate;
  }

  public void setPaiddate(Date paiddate) {
    this.paiddate = paiddate;
  }

  @Basic
  @Column(name = "buyer_name", length = 256)
  public String getBuyerName() {
    return buyerName;
  }

  public void setBuyerName(String buyerName) {
    this.buyerName = buyerName;
  }

  @Basic
  @Column(name = "text", length = 100)
  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  @Basic
  @Column(name = "amount", length = 10)
  public Integer getAmount() {
    return amount;
  }

  public void setAmount(Integer amount) {
    this.amount = amount;
  }

  @Basic
  @Column(name = "value", length = 12, precision = 2)
  public Double getValue() {
    return value;
  }

  public void setValue(Double value) {
    this.value = value;
  }

  @Basic
  @Column(name = "origamount", length = 10)
  public Integer getOrigamount() {
    return origamount;
  }

  public void setOrigamount(Integer origamount) {
    this.origamount = origamount;
  }

  @Basic
  @Column(name = "origvalue", length = 12, precision = 2)
  public Double getOrigvalue() {
    return origvalue;
  }

  public void setOrigvalue(Double origvalue) {
    this.origvalue = origvalue;
  }

  @Basic
  @Column(name = "description", length = 2000)
  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @Basic
  @Column(name = "description1", length = 2000)
  public String getDescription1() {
    return description1;
  }

  public void setDescription1(String description1) {
    this.description1 = description1;
  }

  @Basic
  @Column(name = "prolonged", nullable = false, length = 1)
  public Boolean getProlonged() {
    return prolonged;
  }

  public void setProlonged(Boolean prolonged) {
    this.prolonged = prolonged;
  }

  @Basic
  @Column(name = "stornoreason", length = 256)
  public String getStornoreason() {
    return stornoreason;
  }

  public void setStornoreason(String stornoreason) {
    this.stornoreason = stornoreason;
  }

  @Basic
  @Column(name = "discountrate", length = 7, precision = 4)
  public Double getDiscountrate() {
    return discountrate;
  }

  public void setDiscountrate(Double discountrate) {
    this.discountrate = discountrate;
  }

  @Basic
  @Column(name = "discountreason", length = 256)
  public String getDiscountreason() {
    return discountreason;
  }

  public void setDiscountreason(String discountreason) {
    this.discountreason = discountreason;
  }

  @Basic
  @Column(name = "billno", length = 10)
  public Integer getBillno() {
    return billno;
  }

  public void setBillno(Integer billno) {
    this.billno = billno;
  }

  @Basic
  @Column(name = "pay_methodid", length = 8)
  public String getPayMethodid() {
    return payMethodid;
  }

  public void setPayMethodid(String payMethodid) {
    this.payMethodid = payMethodid;
  }

  @Basic
  @Column(name = "storno_userid", length = 10)
  public Integer getStornoUserid() {
    return stornoUserid;
  }

  public void setStornoUserid(Integer stornoUserid) {
    this.stornoUserid = stornoUserid;
  }

  @Basic
  @Column(name = "maison", length = 1)
  public Boolean getMaison() {
    return maison;
  }

  public void setMaison(Boolean maison) {
    this.maison = maison;
  }

  @Basic
  @Column(name = "canceldate", length = 23)
  public Date getCanceldate() {
    return canceldate;
  }

  public void setCanceldate(Date canceldate) {
    this.canceldate = canceldate;
  }

  @Basic
  @Column(name = "valid_from", length = 23)
  public Date getValidFrom() {
    return validFrom;
  }

  public void setValidFrom(Date validFrom) {
    this.validFrom = validFrom;
  }

  @Basic
  @Column(name = "coupon_comment", length = 256)
  public String getCouponComment() {
    return couponComment;
  }

  public void setCouponComment(String couponComment) {
    this.couponComment = couponComment;
  }
}

and
CTCouponHistoryEntity.java
Code:
package at.tac.ra.ng.dl.entities;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
* @author michael
* @version 0.1.0.0 02.10.2006
*/
@Entity
@Table(name = "C_T_COUPON_HISTORY")
public class CTCouponHistoryEntity  implements Serializable {
  private Integer couponHistoryid;
  private Integer userid;
  private Integer couponStatusid;
  private Double value;
  private Date changedate;
  private String couponno;
  private String description;
  private CTCouponsEntity couponid;

  @Id
  @Column(name = "coupon_historyid", nullable = false, length = 10)
  public Integer getCouponHistoryid() {
    return couponHistoryid;
  }

  public void setCouponHistoryid(Integer couponHistoryid) {
    this.couponHistoryid = couponHistoryid;
  }

  @Basic
  @Column(name = "userid", nullable = false, length = 10)
  public Integer getUserid() {
    return userid;
  }

  public void setUserid(Integer userid) {
    this.userid = userid;
  }

  @Basic
  @Column(name = "coupon_statusid", length = 10)
  public Integer getCouponStatusid() {
    return couponStatusid;
  }

  public void setCouponStatusid(Integer couponStatusid) {
    this.couponStatusid = couponStatusid;
  }

  @Basic
  @Column(name = "value", length = 12, precision = 2)
  public Double getValue() {
    return value;
  }

  public void setValue(Double value) {
    this.value = value;
  }

  @Basic
  @Column(name = "changedate", length = 23)
  public Date getChangedate() {
    return changedate;
  }

  public void setChangedate(Date changedate) {
    this.changedate = changedate;
  }

  @Basic
  @Column(name = "couponno", length = 25)
  public String getCouponno() {
    return couponno;
  }

  public void setCouponno(String couponno) {
    this.couponno = couponno;
  }

  @Basic
  @Column(name = "description")
  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @ManyToOne
  @JoinColumn(name = "couponid", referencedColumnName = "couponid")
  public CTCouponsEntity getCouponid() {
    return couponid;
  }

  public void setCouponid(CTCouponsEntity couponid) {
    this.couponid = couponid;
  }
}


Then there is a Stateless SessionBean for finding these Coupons:
CouponBean.java
Code:
/**
*
* @author michael
* @version 0.1.0.0 02.10.2006
*/
package at.tac.ra.ng.bl.coupon;

import at.tac.ra.ng.dl.entities.CTCouponsEntity;

import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

@Stateless(name = "CouponEJB")
@Remote(Coupon.class)
public class CouponBean implements Coupon {
  @PersistenceContext
  EntityManager manager;


  public CouponBean() {
  }

  public List<CTCouponsEntity> listCoupons() {
    return manager.createQuery("SELECT c FROM CTCouponsEntity c").getResultList();
  }

  public CTCouponsEntity findCouponByID(Integer couponID) {
    Query q = manager.createQuery("SELECT c FROM CTCouponsEntity c WHERE c.couponid = :couponid");
    q.setParameter("couponid", couponID);
    return (CTCouponsEntity) q.getSingleResult();
  }

  public CTCouponsEntity findCouponByNo(String couponNo) {
    Query q = manager.createQuery("SELECT c FROM CTCouponsEntity c WHERE lower(c.couponno) like :couponno");
    q.setParameter("couponno", couponNo.toLowerCase());
    return (CTCouponsEntity) q.getSingleResult();
  }
}

and there is a Client:
Client.java
Code:
import at.tac.ra.ng.bl.coupon.Coupon;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
* @author michael
* @version 0.1.0.0 02.10.2006
*/
public class Client {
  public static void main(String[] args) {
    try {
      Context ctx = new InitialContext();
      Coupon c = (Coupon) ctx.lookup("CouponEJB/remote");
      System.out.println(c.findCouponByNo("Beauty00001/01").getHistory().get(1).getDescription());
    } catch (NamingException e) {
      e.printStackTrace();
    }

  }
}

However, when running this Code I get an Exception:
Code:
log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: at.tac.ra.ng.dl.entities.CTCouponsEntity.history, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
   at org.hibernate.collection.PersistentBag.get(PersistentBag.java:398)
   at Client.main(Client.java:16)
Disconnected from the target VM, address: '127.0.0.1:3997', transport: 'socket'

Process finished with exit code 1


What do I do wrong?

I've read somewhere on googleing, that I need to attach the session to the current thread somehow, need to aquire a new one or something similar?!?

nomike aka Michael Postmann

_________________
I haven't lost my mind, I know exactly where I left it!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 03, 2006 5:56 am 
Newbie

Joined: Tue Oct 03, 2006 4:41 am
Posts: 2
Location: Pinkafeld/Bgld./Austria
Well, I think I found the reason for my Problem:

I'm getting an Entity from JBoss.
On the Client my Entity is now detached.
So I could not do lazy loading.

So possible options seem to be:
  1. Pass the entity back to JBoss (SessionBean), let it persist the entity again, fetch the associated entities and return them to the client.
  2. Ask a SessionBean in JBoss to fetch the associated entities and return them to the client.
  3. Persist the Object on the client side.

Option 3. seems to be the prefered one here.
So where do I get a EntityManager on the Client side to call em.persist()?

_________________
I haven't lost my mind, I know exactly where I left it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.