0 Replies Latest reply on Dec 11, 2006 1:02 PM by idtechnology

    Persisting an Entity Bean with a null attribute throws Trans

    idtechnology

      Hi there,
      I'm a JBOSS/EJB3 newbie.
      I have installed JBOSS 4.0.5 GA, JDK 1.5.0_05, Eclipse 3.2+JBOSSIDE 1.6.0 plugin. MYSQL 5, mysql-connector-java-3.1.13 driver

      I'm trying to persist some entity beans under transaction. One of these beans has a null (non-key) attribute. Either When I call persist or when I perform a readonly query on that object I get a

      javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction

      @Entity
      @Table(name = "person", catalog = "take2", uniqueConstraints = {})
      public class Person implements java.io.Serializable {
      // Fields
      protected long idPerson;
      protected String name;
      protected String srname;
      protected String gender;
      protected Integer bthdateDD;
      protected Integer bthdateMm;
      protected Integer bthdateyyyy;
      protected String sInsCOde;
      protected Long job;
      protected Long bthplace;
      public static final String GENDER_M = "M";
      public static final String GENDER_F = "F";
      ....
      }

      Here I have a managed bean to create an instance of Person that I pass to an transacted EJB to manage persistence under transaction.

      private PersonBean createDefaultPerson()
      {
      PersonBean pb = new PersonBean();
      pb.setSrName("Siciliano");
      pb.setName("Andrea");
      pb.setGender(Persona.SESSO_M);
      pb.setBthDateDD(19);
      pb.setBthDateMM(11);
      //BthDateyyyy is not set so it is null in the managed bean
      pb.setAge(33);
      pb.setBthplace(9852L);
      .....
      return pb;
      }

      then the ejb persists the bean but I get the exception..

      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public MyBean insDati(PersonBean abean)
      {
      ...
      ...
      entitymanager.persist(abean);
      ..
      ..
      }

      the exception does not triggers if the PersonaBean instance has all its attributes set to not null values.
      The database does not have a not null constraints on the non key values.
      +-----------------+------------------+------+-----+---------+
      | Field | Type | Null | Key | Default | Extra|
      +-----------------+------------------+------+-----+---------+
      | idPerson | bigint(20) | NO | PRI | NULL | auto_increment |
      | name | varchar(45) | YES | | NULL |
      | srname | varchar(45) | YES | | NULL |
      | gender | varchar(1) | YES | | NULL |
      | bthdateDD | int(10) unsigned | YES | | NULL
      | bthdateMM | int(10) unsigned | YES | | NULL
      | bthdateyyyy | int(10) unsigned | YES | | NULL
      | sInsid | varchar(16) | YES | | NULL |
      | job | bigint(20) | YES | | NULL |
      | bthplace | bigint(20) | YES | | NULL |
      +-----------------+------------------+------+-----+---------+

      What's goin' on?

      thanks
      :G