File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

no error but entity is not persisted

 
dan volfman
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
Eclipse IDE Firefox Browser Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dan volfman
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
Eclipse IDE Firefox Browser Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic