aspose file tools *
The moose likes EJB and other Java EE Technologies and the fly likes no error but entity is not persisted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "no error but entity is not persisted" Watch "no error but entity is not persisted" New topic
Author

no error but entity is not persisted

dan volfman
Greenhorn

Joined: Jan 15, 2010
Posts: 6
hi,

i have the following bean:
@Stateless
public class LibraryServicesBean implements LibraryServicesLocal {
@PersistenceContext
EntityManager em;

public void test() {

Category cat = new Category();
cat.setCategoryName("aaa");
em.persist(cat);

}
}

(Category is a normal entity bean which i can persist when i dont use session beans.)

and the following code in the client:

@EJB
static LibraryServicesLocal library;

public static void main(String[] args) {

library.test();
}

I use netbeans and its local DB.

when i run the application i get no exceptions but the entity is not persisted. (i check the db and there are no tables).

any ideas of what i'm doing wrong?

thank you.
Yair Ivan
Greenhorn

Joined: Jul 13, 2008
Posts: 15

Hi Dan:

@Stateless
public class LibraryServicesBean implements LibraryServicesLocal {

@PersistenceContext EntityManager em;

public void addCategory(Category cat) {
em.persist(cat);
}
}

The client (Suppose what client is managed-container) :

@EJB private LibraryServicesLocal library;

public static void main(String[] args) {

Category cat = new Category();
cat.setCategoryName("aaa");
library.addCategory(cat );
}

If persist() is called within a transaction, the insert may happen immediately, or it may be queued until the end of the transaction, depending on the flush mode. You may force the insertion manually within a transaction by calling the flush() method.

By.


SCJP 6.0, SCBCD 5.0, SCDJWS 5.0
dan volfman
Greenhorn

Joined: Jan 15, 2010
Posts: 6
hi
i tried adding flush() after the persist but it is still the same.

just a thought: shouldnt the tables be created anyway? even before i persist entities?
my db is empty, no tables.
is it possible i'm doing something wrong in the persistence unit?

here's the code:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="LibraryApp-ejbPU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<non-jta-data-source/>
<class>entities.Category</class>
<class>entities.Book</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.jdbc.user" value="nbuser"/>
<property name="toplink.jdbc.password" value="nbuser"/>
<property name="toplink.jdbc.url" value="jdbc:derby://localhost:1527/LibraryDB"/>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
Yair Ivan
Greenhorn

Joined: Jul 13, 2008
Posts: 15

Hi:

In that case definitely have to check persistence.xml.

If you define the attribute transaction-type="JTA" also need datasource.

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="LibraryApp-ejbPU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>jdbc/MyDataSource</jta-data-source>
<properties>
<property name="toplink.logging.level" value="INFO"/>
<property name="toplink.jdbc.user" value="nbuser"/>
<property name="toplink.jdbc.password" value="nbuser"/>
<property name="toplink.jdbc.url" value="jdbc:derby://localhost:1527/LibraryDB"/>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>

See the following "TopLink JPA Persistence Unit Extensions":

http://www.oracle.com/technology/products/ias/toplink/JPA/essentials/toplink-jpa-extensions.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: no error but entity is not persisted