Re: CMP failed with 3 layered Object structure
No my contact class is just out of a few strings like firstName, surName, street, city, zipcode, country and so on. -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655582.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Your haven't defined the transaction scope on your ejb methods. Try adding the annotation @TransactionAttribute(TransactionAttributeType.REQUIRED) to your getCustomer method... @Override @TransactionAttribute(TransactionAttributeType.REQUIRED) public Customer getCustomer( String customerId, String sessionId) throws OpenflowException{ Session session = sessionBean.isSessionValid(sessionId); if(session!=null) { Customer customer = (Customer) entityManager.find(Customer.class, customerId); if(customer==null) { throw new OpenflowException(Customer does not exists +customerId); } customer.getContactList(); return customer; } return null; } -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655586.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
I am not @ work. If I am annotate this method, will I need still the getContacts().size()-method, to get no lazyLoading Exception? -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655588.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
I may have misunderstood your current issue. It seems you are able to get a successful response if you call the getContactList().size() method before returning from your method. If that is the case then adding the @TransactionAttribute will probably not change anything. The issue you are having is really common and happens everywhere you use JPA entities as objects that you pass across transaction boundaries. As soon as the JPA entity is detached, which happens when the transaction commits at the end of your method, you can't automatically load lazy-loaded attributes. Because your method is a webservice operation, the response gets marshalled by JAXB into XML which is when the getContactList() call would be happening after the transaction has committed. The same issue would also happen if you got a Customer entity in a servlet within a transaction and then passed it to a JSP page for rendering and that JSP page tried to call the getCustomerList method. There are several ways of solving this problem. One way in web applications is to use a servlet filter to start and end the transaction so that the JSP page rendering would happen within the same transaction that the Customer entity was retrieved from the database. In this case, the Customer entity is not detached and so the JSP page call to getContactList() would cause the EntityManager to retrieve the data from the database. I'm not sure if there is a similar mechanism you could use for a webservice. Perhaps there is some interceptor you could use around the getCustomer method that could start and commit the transaction after the XML serialization has occurred. Another way is to design your app so you don't use JPA entities as data transfer objects (DTOs). Create another package of dto java beans that are used explicitely for this purpose. Then create stateless ejbs that populate DTOs from your JPA entities within a transaction. The DTO object would then be returned from your webservice and be guaranteed to have all the state already loaded. Although this way sounds like alot more work and i may get flamed for suggesting it, i personally prefer this approach because it takes away the uncertainty about what is or isn't loaded that you always get with JPA entities. Another way is to do as you are currently doing and make sure you call methods that you think are going to be called on the Customer object after you return it. As you have said, this way is not great because you have to guess what lazy loaded attributes the caller of your method is interested in. If you don't like using the method calls to load the lazy-loaded attributes, an alternative is to create a JPA query using the FETCH keyword to perform an eager fetch of the lazy loaded attributes. eg. entityManger.createQuery(SELECT c FROM Customer c LEFT JOIN FETCH c.contactList WHERE c.id = :id).setParameter(id, customerId).getSingleResult(); -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655609.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
hi guys, i have still a problem with this lazy loading issue. I changed in my persistence.xml transaction-type to JTA to manage all transactions with the container. But the LazyLoading ISSUE is still alive :( Can anyone help me? -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p469.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Hi, You need to init the relationships before going out of a transaction scope. You can either use a fetch query or simply call getMyRelationShip() because returning the entity. - Romain Le 12 juin 2012 13:50, georgenis georgek...@googlemail.com a écrit : hi guys, i have still a problem with this lazy loading issue. I changed in my persistence.xml transaction-type to JTA to manage all transactions with the container. But the LazyLoading ISSUE is still alive :( Can anyone help me? -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p469.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Sorry Romain, i am a beginner and do not really understand what you tried to say to me?! I have to implement an method which fetch all roles? -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655561.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Just call your getter before returning your entity. Le 12 juin 2012 16:31, georgenis georgek...@googlemail.com a écrit : Sorry Romain, i am a beginner and do not really understand what you tried to say to me?! I have to implement an method which fetch all roles? -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655561.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Which getter? I have a similiar method just for customers and contacts. I tried it like this: public Customer getCustomer( String customerId, String sessionId) throws Exception{ Session session = sessionBean.isSessionValid(sessionId); if(session!=null) { Customer customer = (Customer) entityManager.find(Customer.class, customerId); if(customer==null) { throw new Exception(Customer does not exists +customerId); } customer.getContactList(); return customer; } return null; } Before I return my Entity i call the getter-Method which should load all Contacts in my Customer Object. But my session is still closed?! I think i didnt understand you. -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655563.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Yes, I am using an EJB. I pasted my full class on following link: http://pastebin.com/vVcZx4aB I am not allowed to share it completely :-( Its a company training project not for external people. I hope you can help me as much as possible. -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655566.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Can you debug the returned list please? Le 12 juin 2012 17:35, georgenis georgek...@googlemail.com a écrit : Yes, I am using an EJB. I pasted my full class on following link: http://pastebin.com/vVcZx4aB I am not allowed to share it completely :-( Its a company training project not for external people. I hope you can help me as much as possible. -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655566.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Hi romain, i debugged my application, when i call the method getContactList().size() i get no lazyloading exception. I think its not the right way to develope. Is there any possibility to develope it in the right way? -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655569.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Hi, does contact has relationships too? If yes same issue. Le 12 juin 2012 22:18, georgenis georgek...@googlemail.com a écrit : Hi romain, i debugged my application, when i call the method getContactList().size() i get no lazyloading exception. I think its not the right way to develope. Is there any possibility to develope it in the right way? -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655569.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
Thanks for that hint :-) I am a JEE beginner. My Class is an EJB ;-) I fixed it in persistence.xml with Transaction-Type = JTA. -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655352.html Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: CMP failed with 3 layered Object structure
On Jun 3, 2012, at 11:22 PM, georgenis wrote: Thanks for that hint :-) I am a JEE beginner. My Class is an EJB ;-) I fixed it in persistence.xml with Transaction-Type = JTA. Glad it's working. Feel free to lean on the list as much as you like while learning. :) That jpa concepts doc came from seeing the same question quite a few times. The doc grew from the answers. A good example of how questions and users drive the project. -David
Re: CMP failed with 3 layered Object structure
On Jun 2, 2012, at 2:53 AM, georgenis wrote: I thought the container manage all database connections?! It depends. There are two types of JPA, RESOURCE_LOCAL were the application does the management and TRANSACTION where the container does the management. A basic overview: http://openejb.apache.org/jpa-concepts.html This looks like a lazy-loading/detach problem. The relationship is marked as lazy and therefore not loaded when the Entity is pulled from the database via the EntityManager. To cause the lazily loaded references to be loaded, the collection has to be referenced while the transaction (either EntityTransaction or JTA Transaction) is still in progress. If you can post the web service class I can give some better pointers on the best place to do that. -David
Re: CMP failed with 3 layered Object structure
Hi, here is a short preview of my method: @Override public RoleInformation getRole(String roleId, String sessionId) throws OpenflowException { Session session = sessionBean.isSessionValid(sessionId); if(session!=null) { RoleInformation roleInformation = (RoleInformation) entityManager.find(RoleInformation.class, roleId); if(roleInformation==null) throw new OpenflowException(Role does not exists +roleId); return roleInformation; } return null; } The entityManager will be injected by @PersistenceContext(unitName=myUnit). -- View this message in context: http://openejb.979440.n4.nabble.com/CMP-failed-with-3-layered-Object-structure-tp4655311p4655315.html Sent from the OpenEJB User mailing list archive at Nabble.com.