[ajug-members] help with ejb persistence
Ramesh Rajamani
rameshrajamani at hotmail.com
Thu Mar 24 08:51:15 EDT 2011
Hello ,
I love the way the new version is designed the EJB 3.1 greater and Servlet 3.0 , we can bundle the war file. There is no need for the EAR file. But there are some organisation still wanting to build EAR. What is your thought?
If i own a project , there is either one of them convention over configuration or Injection and not mixed together. I also prefer the annotation (Injection) which reduce many configuration while building the apps.
Regards,
Ramesh Rajamani
Date: Wed, 23 Mar 2011 14:57:16 -0400
From: rworsnop at gmail.com
To: ajug-members at ajug.org
Subject: Re: [ajug-members] help with ejb persistence
Mike,
The factory is a singleton that gets created at startup, based on your persistence.xml. You shouldn't be creating a new one in your DAO method.
You can then inject an entity manager into an EJB session bean or you can look it up by a JNDI name you choose in persistence.xml (see JBoss doc). In most cases there's no need for you to see the factory at all.
There's no need to start/commit/rollback transactions yourself. If you use EJB session beans, transaction boundaries can be created for you. If you use Spring, you can use the @Transactional annotation to get the same behavior.
I would suggest getting your application in line with normal JPA development practices before tackling the problem you posted. Not creating the factory in your DAO method is the most important step you need to take, although I would recommend declarative transaction management because it makes your code tidier and less error-prone.
Rob.
On Wed, Mar 23, 2011 at 2:07 PM, mike barnes <mdb3624 at gmail.com> wrote:
I have developed a webservice on JBoss 5.x using EJB3 to do my database magic and am unable to save information to the database. I am able though to retrieve information. After reviewing the files below suggest what I need to change to get persistence to work....
----------------------- Persistence.xml ---------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistence_unit_local" transaction-type="JTA">
<jta-data-source>java:/SybaseCssDS</jta-data-source>
<class>com.weenergies.cs.services.myaccount.common.account.persistence.AccountDbEntity</class>
<class>com.weenergies.cs.services.myaccount.common.customer.persistence.CustomerDbEntity</class>
<class>com.weenergies.cs.services.myaccount.common.paymentaccount.persistence.PaymentAccountDbEntity</class>
<class>com.weenergies.cs.services.myaccount.common.bill.persistence.BillDbEntity</class>
<class>com.weenergies.cs.services.myaccount.common.bill.persistence.BillSummaryDbEntity</class>
<class>com.weenergies.cs.services.myaccount.common.payment.persistence.PaymentDbEntity</class>
<class>com.weenergies.cs.services.myaccount.common.payment.persistence.ProcessedPaymentDbEntity</class>
<class>com.weenergies.cs.services.myaccount.common.payment.persistence.PendingPaymentDbEntity</class>
<properties>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
</properties>
</persistence-unit>
</persistence>
--------------------------------------------------------------------------------------------
----------- AccountDAO -----------------------------------------------------------
public class AccountDAO {
public AccountDbEntity updateAccount (Account account) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence_unit_local");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
AccountDbEntity persistenceUnit = getAccount(account.getAttributes());
if (persistenceUnit != null) {
persistenceUnit.setAccountClass(account.getAccountClass());
persistenceUnit.setAccountNickname(account.getAccountNickname());
persistenceUnit.setAddressHouseNumber(account.getAddressHouseNumber());
persistenceUnit.setAddressMultipleDwellingType(account.getAddressMultipleDwellingType());
persistenceUnit.setAddressServiceCity(account.getAddressServiceCity());
persistenceUnit.setAddressServiceState(account.getAddressServiceState());
persistenceUnit.setAddressServiceZip(account.getAddressServiceZip());
persistenceUnit.setAddressStreetName(account.getAddressStreetName());
persistenceUnit.setAddressStreetSuffix(account.getAddressStreetSuffix());
persistenceUnit.setBankPlanCode(account.getBankPlanCode());
persistenceUnit.setBillCycleId(account.getBillCycleId());
persistenceUnit.setCoCode(account.getCoCode());
// TODO: need to be able to set the customer from the account
//persistenceUnit.setCustomer(new CustomerDAO().getCustomer(account.getAttributes().getCustomer().getCustomerId());
persistenceUnit.setCustomerName(account.getAttributes().getCustomer().getCustomerName());
persistenceUnit.setDisconnectNoticeFlag(account.getDisconnectNoticeFlag());
persistenceUnit.setFinalBillFlag(account.getFinalBillFlag());
persistenceUnit.setFireNumberId(account.getFireNumberId());
persistenceUnit.setFlagPaperFreeBilling(account.isPaperFreeBillingFlag() ? "Y" : "N");
persistenceUnit.setFlagReminderEmail(account.isReminderEmailFlag() ? "Y" : "N");
persistenceUnit.setId(account.getAttributes().getAccountNumber());
try {
em.merge(persistenceUnit);
em.getTransaction().commit(); // flush occurs
} catch (Exception e) {
em.getTransaction().rollback();
e.printStackTrace();
}
em.close();
entityManagerFactory.close();
}
return persistenceUnit;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The em.merge appears to fail silently (i.e. no errors are thrown.
That am I doing wrong, I think that the transaction manager is not being set up.
Thanks
Mike
_______________________________________________
ajug-members mailing list
ajug-members at ajug.org
http://www.ajug.org/mailman/listinfo/ajug-members
_______________________________________________ ajug-members mailing list ajug-members at ajug.org http://www.ajug.org/mailman/listinfo/ajug-members
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.ajug.org/pipermail/ajug-members/attachments/20110324/7440cec3/attachment.html
More information about the ajug-members
mailing list