无法初始化代理 - 没有会话

时间:2013-10-31 18:45:32

标签: hibernate session lazy-loading marshalling

我正在尝试Marshall一个对象,首先我创建这个对象(在另一个类中),然后我将它Marshall到一个XML文档。虽然这样做我得到错误:“无法初始化代理 - 没有会话”。

我的对象已被创建,因为它已放入我的数据库...

有人可以帮我解决这个问题吗???

发件人类

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();
    }
}

段落类

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;
}

1 个答案:

答案 0 :(得分:0)

尝试编组时,不要将实体对象直接传递给编组程序。当marshaller试图从对象获取一个惰性属性时,它将没有会话来检索惰性属性,此时它会抛出此异常。

解决方案:1。我的建议是,创建另一个与原始实体类似的数据对象/ POJO,将实体的所有属性复制到此POJO中,并将此POJO发送给编组器为您创建一个xml

  1. 或者使lazy = false。