Forums Register Login

An amazing issue when using in uuid generator

+Pie Number of slices to send: Send
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;
}
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 819 times.
Similar Threads
Problem whne used custom squence name in JPA (hibernate) annotations
Beginner needs help with Spring + JPA + Transactions
update using JPA
COMMIT is not allowed in a subordinate session
Passing EntityManager reference between methods doesn't work as expected
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:14:36.