Hey,
My application server is Jboss ver – 4.2
I have a stateless (facade) that call to a DAO (simple pojo class ).
Due to some restrictions (I need to create schema dynamically) I cant use injection , so I create the factory and bind it to the JNDI.
The issue that immediately after I save an Object I call to find but I get null although that the Object will commit to the DB after the transaction (the transaction boundaries are in the stateless) will end.
This behavior happen only when the ID is generated in uuid strategy in case that it generated in @GeneratedValue it will find the object.
In case that I use this code to find - em.find(Hotel.class, newHotel.getOrmID()); its okey in both cases , but I cant use this attitude because in the same transaction in another place I can call to another DAO and I want to see the Objects that I already saved .
DAO:
Code:
public class TestSimpleDao {
private EntityManager em = null;
public EntityManager getEntityManager(){
EntityManagerFactory emf = null;
try {
emf = (EntityManagerFactory) new InitialContext().lookup("java:/Demo");
} catch (NamingException e) {
e.printStackTrace();
}
em = emf.createEntityManager();
return em;
}
public Hotel saveHotel(Hotel newHotel) {
getEntityManager().persist(newHotel);
newHotel = getEntityManager().find(Hotel.class, newHotel.getOrmID());
return newHotel;
}
}
Code:
<persistence>
<persistence-unit name="Demo">
<jta-data-source>java:/DemoDS</jta-data-source>
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultComponentSafeNamingStrategy" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
Hotel:
Code:
@Entity
@Table(name = "HOTELS")
public class Hotel implements Serializable {
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name="managed_element_id")
protected String ormID = null;
private String name;
private String address;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn
private List<Room> rooms = new LinkedList<Room>();
private Hotel() {
}
public Hotel(String name, String address) {
this.name = name;
this.address = address;
}
…