Warri
Warri

Reputation: 217

could not initialize proxy - no Session

I'm trying to Marshall an object, first I create this object (in another class) and then I Marshall it to a XML document. While doing this I'm getting the error: "could not initialize proxy - no Session".

My object has been created cause it has been put in my database...

Can someone help me with this problem please???

Sender class

public class SenderPassage {
public static void main(String[] args) throws JMSException, JAXBException, IOException        {
    ActiveMQConnectionFactory connectionFactory = new    ActiveMQConnectionFactory("tcp://localhost:61616");
    Connection connection = connectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue("PASSAGE");
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

    JAXBContext context = JAXBContext.newInstance(Passage.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Passage passage = new Passage().RandomPassage();

    System.out.println(passage.toString());
    System.out.println("Marshalling...");

    Writer writer = new FileWriter("passage.xml");
    marshaller.marshal(new Passage().RandomPassage(), writer);

    TextMessage message = session.createTextMessage(writer.toString());
    producer.send(message);

    producer.close();
    session.close();
    connection.close();
    }
}

Passage class

public Passage RandomPassage() throws UnknownHostException {
    Session session = persistentie.HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = session.beginTransaction();
    Random random = new Random();

    //random festival genereren
    Query queryMaxFestivalID = session.createQuery("select count(*) from Festival");
    int maxFestivalID = ((Long) queryMaxFestivalID.uniqueResult()).intValue();
    int festivalID = random.nextInt(maxFestivalID) + 1;

    //max zoneID voor random waarde
    Query queryMaxZoneID = session.createQuery("select count(*) from Zone zone where zone.festival.id = :fID");
    queryMaxZoneID.setParameter("fID", festivalID);
    //max polsbandID voor random waarde
    Query queryMaxPolsbandID = session.createQuery("select count(*) from RFID rfid where rfid.festival.id = :fID");
    queryMaxPolsbandID.setParameter("fID", festivalID);
    int maxZoneID = ((Long) queryMaxZoneID.uniqueResult()).intValue();
    int maxPolsbandID = ((Long) queryMaxPolsbandID.uniqueResult()).intValue();

    //passageID
    Query queryMaxPassageID = session.createQuery("select count(*) from Passage passage where passage.RFID.festival.id = :fID");
    queryMaxPassageID.setParameter("fID", festivalID);
    int maxPassageID = ((Long) queryMaxPassageID.uniqueResult()).intValue();

    Passage passage = new Passage();

    //generate polsbandID + set
    int polsbandID = random.nextInt(maxPolsbandID) + 1;
    Query getPolsband = session.createQuery("select rfid from RFID rfid where rfid.id = :id");
    getPolsband.setParameter("id", polsbandID);
    RFID rfid = (RFID) getPolsband.uniqueResult();
    passage.setRFID(rfid);
    //generate zoneID + set
    int zoneID = random.nextInt(maxZoneID) + 1;
    Query getZone = session.createQuery("select zone from Zone zone where zone.id = :id");
    getZone.setParameter("id", zoneID);
    Zone zone = (Zone) getZone.uniqueResult();
    passage.setZone(zone);
    //generate type in of uit + set
    boolean type = random.nextBoolean();
    passage.setType(type);
    //set date
    passage.setTijdstip(new Date());

    session.saveOrUpdate(passage);

    tx.commit();

    //mongoDB(polsbandID, zoneID, passage.getTijdstip(), type);


    return passage;
}

Upvotes: 0

Views: 1658

Answers (1)

Zeus
Zeus

Reputation: 6566

When trying to marshall, do not pass the entity object directly to the marshaller. When marshaller tries to get a lazy attribute from the object, it will not have session to retrieve the lazy attribute, at that point it throws this exception.

Solution: 1. My advise, create another Data Object/POJO which is similar to that of the original entity, copy all the attributes of the entity into this POJO, and send this POJO to the marshaller to create an xml for you.

  1. Or make the lazy = false.

Upvotes: 1

Related Questions