Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

no error but entity is not persisted

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
Eclipse IDE Firefox Browser Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic