Persistence Manager Strange Behaviour
aidan_b5 Feb 24, 2006 8:38 AMI cannot get the persistence manager to persist the objects in the following code. No exception is thrown (on persist), the objects are simply not persisted.
@Stateless public class SRSFacadeBean implements SRSFacade{ @PersistenceContext (unitName="srsentity") ...... public void createShip(DynaBean db) throws DelegateException { createEntity(); Ship ship = new Ship(); String imo = (String) db.get("imo"); ship.setImo(imo); DynaProperty dp[] = db.getDynaClass().getDynaProperties(); long currenttime = Calendar.getInstance().getTimeInMillis(); ArrayList<Attribute> ala = new ArrayList<Attribute>(); for(DynaProperty dpp: dp) { String nm = dpp.getName(); if(!nm.equals("imo")) { Attribute attribute = new Attribute(); String vl = (String) db.get(nm); //get attribute name AttributeName attributename = null; try { attributename = (AttributeName) manager.createQuery("from AttributeName ana where ana.name = ?1").setParameter(1, nm).getSingleResult(); } catch(NoResultException e) { log.info("AttributeName not found, creating:" + nm); attributename = new AttributeName(); attributename.setName(nm); } catch(Exception e) { log.debug("Multiple AttributeNames (" + nm + ") found, database integrity must be reestablished"); e.printStackTrace(); //TODO: Reestablish database integrity! } //set attribute to attributename mapping + vice versa attribute.setNameid(attributename); attributename.getAttr().add(attribute); //set values attribute.setValue(vl); attribute.setStartdate(currenttime); attribute.setEnddate(-1); attribute.setShip(ship); ala.add(attribute); manager.persist(attributename); manager.persist(attribute); } } ship.setAttributes(ala); manager.persist(ship); } public void createEntity() { AnEntityBean aeb = new AnEntityBean(); aeb.setTotal(111111111); manager.persist(aeb); } }
persistence.xml
<persistence> <persistence-unit name="srsentity"> <jta-data-source>java:/MySqlDS</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
The bizarre thing is that if my test method, createEntity() is called from outside the Bean, it works fine and AnEntityBean is persisted to the database.
If I call it from within this class (as I have at the top of createShip(), it doesn't persist AnEntityBean.
Ship, Attribute and AttributeName are all standard entities mapped to tables, futher code can be posted as required.
This code falls over because each instance of AttributeName is never persisted, therefore the query returns null.
Help would be very much appreciated as I feel I'm tired of banging my head against the proverbial brick wall!