4 Replies Latest reply on Apr 27, 2007 11:53 AM by Dejan Music

    Problem by inserting of entities

    Dejan Music Newbie


      Hi,

      I use JBoss Seam 1.2.1 GA, Jboss 4.0.5 GA and JSF for one project. I have made one new Session bean for inserting/deleting some data in/from database.

      It's look something like this:

      
      @Stateless
      @Name("initialize")
      public class InitializeData implements Initialize
      {
       @PersistenceContext
       private EntityManager em = null;
      
       public void insertData()
       {
       try
       {
      
       PriceCategory cat1 = new PriceCategory("Old", new BigDecimal(3.2));
       PriceCategory cat2 = new PriceCategory("New", new BigDecimal(5.2));
      
       em.persist(cat1);
       em.persist(cat2);
      
       em.flush();
       }
       catch(Exception e)
       {
       e.printStackTrace();
       }
       }
      


      But when I call this method from JSF, I get always this exception by first em.persist()-call:

      
      09:27:45,078 INFO [STDOUT] Hibernate: insert into PriceCategory (priceCategoryId, name, dailyFee, optLock) values (null, ?, ?, ?)
      09:27:45,093 WARN [JDBCExceptionReporter] SQL Error: -104, SQLState: 23000
      09:27:45,093 ERROR [JDBCExceptionReporter] Unique constraint violation: SYS_CT_61 in statement [insert into PriceCategory (priceCategoryId, name, dailyFee, optLock) values (null, ?
      , ?, ?)]
      09:27:45,093 ERROR [STDERR] javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [model.PriceCategory]
      09:27:45,093 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:622)
      09:27:45,093 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
      
      


      I don't really understand what happend there. Perheps my entity bean PriceCategory:

      
      @Entity
      @Table(name = "PriceCategory")
      @Name("pricecategory")
      public class PriceCategory implements Serializable {
      
       @Id
       @GeneratedValue(strategy=GenerationType.IDENTITY)
       @Column(name = "priceCategoryId")
       private Integer id;
      
       @Column(name = "name", nullable = false, length = 20, unique=true)
       private String name;
      
       @Column(name = "dailyFee", nullable = false, precision = 4, scale = 2)
       private BigDecimal dailyFee;
      
       @Version
       @Column(name = "optLock")
       private Integer version;
      ...
      
      


      I am really stuck at this point and without data in database cann't do anything more.

      Could anyone give me some hint for get over it?

      Thanks a lot for every help!


        • 1. Re: Problem by inserting of entities
          Sander Mak Newbie

          My best bet is that insertData() gets called multiple times. How is it invoked?

          • 2. Re: Problem by inserting of entities
            Dejan Music Newbie


            Hi,

            Yes, that's right. Then is normal to get some exception. But when I restart my jboss server and delete all deployed stuff and also content of "data"-folder by my default jboss instance I get also the errors.

            After restarting I assume that jboss should delete database for me; but he doesn't that.

            Is there any way to tell jboss to delete the content of database by restart!?

            p.s. where is actually content of database saved?

            Thx for hints!

            music

            • 3. Re: Problem by inserting of entities
              Sander Mak Newbie

              The answers to those question would depend on the contents of your persistence.xml file, may be you can post that?

              • 4. Re: Problem by inserting of entities
                Dejan Music Newbie

                 

                "Sammy8306" wrote:
                The answers to those question would depend on the contents of your persistence.xml file, may be you can post that?


                Hi,

                I also try a lot of things to do in persistance.xml. My persistance.xml look like this:

                
                <?xml version="1.0" encoding="UTF-8"?>
                <persistence 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"
                 version="1.0">
                
                 <persistence-unit name="video_library" transaction-type="JTA">
                
                 <provider>org.hibernate.ejb.HibernatePersistence</provider>
                 <jta-data-source>java:/video_libraryDatasource</jta-data-source>
                
                 <properties>
                
                 <!-- database connection properties -->
                 <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
                 <property name="hibernate.connection.url" value="jdbc:hsqldb:./db/" />
                 <property name="hibernate.connection.username" value="sa" />
                 <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
                
                 <!-- JDBC connection pool (use the built-in) -->
                 <property name="connection.pool_size" value="5" />
                
                 <!-- Enable Hibernate's automatic session context management -->
                 <property name="hibernate.current_session_context_class" value="thread" />
                
                 <!-- enable OSCacheProvider as second-level cache provider -->
                 <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
                
                 <!-- use query cache (individual queries should still be cacheable) -->
                 <property name="hibernate.cache.use_query_cache" value="true" />
                
                 <!-- with this option we can also completely disable second level cache -->
                 <property name="hibernate.cache.use_second_level_cache" value="true" />
                
                 <!-- force hibernate to cache all data in humanreadable format -->
                 <property name="hibernate.cache.use_structured_entries" value="true" />
                
                 <!-- Echo all executed SQL to stdout -->
                 <property name="show_sql" value="true" />
                
                 <!-- preety print of all SQL-Statements -->
                 <property name="hibernate.format_sql" value="false" />
                
                 <!-- Drop and re-create the database schema on startup -->
                 <property name="hbm2ddl.auto" value="create-drop" />
                
                 <!-- enable generation of statistics -->
                 <property name="hibernate.generate_statistics" value="true" />
                
                 <property name="jboss.entity.manager.factory.jndi.name" value="java:/video_libraryEntityManagerFactory"/>
                
                 <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
                
                
                 </properties>
                
                 </persistence-unit>
                
                </persistence>
                


                I think that the action "create-drop" should do some stuff for me. At least I have always use it in row hibernate and I have not any problems with existing database.

                Thx for responding!