Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

An amazing issue when using in uuid generator

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Immediately after I save an Object I call to find ,but I get null although that the Object will save 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:

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


<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:

@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;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic