Hibernate не фиксирует данные - PullRequest
2 голосов
/ 24 октября 2011

Я пытаюсь написать веб-сервис для хранения некоторых данных, используя jBoss 4.2.3 и Oracle.Я предполагаю использовать Hibernate с CMT и мне кажется, что он не совершает транзакцию.У меня есть следующий код: Бин :

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ZzzBean implements ZzzI {

    public ZzzBean() {
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public int addZzz() {
        try {
            Zzz z = new Zzz();
            z.setA("a");
            z.setI(new BigDecimal(11));
            HibernateUtil.getSessionFactory().getCurrentSession().save(z);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 1;
    }
}

Интерфейс :

@Local
public interface ZzzI {
    int addZzz();
}

Моя фабрика сеансов класс:

public class HibernateUtil {

    private static SessionFactory sessionFactory;
    private static Configuration configuration;

    static {
        try {
            configuration = new AnnotationConfiguration().configure(); 
            sessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

hibernate.cfg.xml имеет следующее:

<session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@//my_host</property>
    <property name="hibernate.connection.username">my_user</property>
    <property name="hibernate.connection.password">my_pass</property>

    <property name="show_sql">true</property>
    <property name="current_session_context_class">jta</property>

    <property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
    <property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>

    <mapping resource="com/example/Zzz.hbm.xml" />
</session-factory>

Отображение файл:

<hibernate-mapping>
    <class name="com.example.Zzz" table="ZZZ">
        <id name="i" type="big_decimal">
            <column name="i" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="a" type="string">
            <column name="a" length="20" />
        </property>
    </class>
</hibernate-mapping>

Entity класс:

@Entity
@Table(name = "ZZZ")
public class Zzz implements Serializable {

    private static final long serialVersionUID = -4165930294512113400L;

    private BigDecimal i;
    private String a;

    public Zzz(){}

    @Id
    @Column(name = "i", unique = true, nullable = false, precision = 22, scale = 0)
    public BigDecimal getI() {
        return this.i;
    }

    @Column(name = "a", length = 20)
    public String getA() {
        return this.a;
    }

    public void setI(BigDecimal i) {
        this.i = i;
    }

    public void setA(String a) {
        this.a = a;
    }
}

И веб-служба код:

@WebService(name = "Zzz", serviceName = "Zzz")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@Stateless
public class ZzzWS {
    @EJB(beanName = "ZzzBean")
    ZzzI z;

    @WebMethod
    public int addZzz() {
        return z.addVisit();
    }

}

Когда я вызываю метод веб-службы, у меня выводится следующее:

13:43:52,671 INFO  [Version] Hibernate Annotations 3.2.1.GA
13:43:52,682 INFO  [Environment] Hibernate 3.2.4.sp1
13:43:52,686 INFO  [Environment] hibernate.properties not found
13:43:52,687 INFO  [Environment] Bytecode provider name : javassist
13:43:52,691 INFO  [Environment] using JDK 1.4 java.sql.Timestamp handling
13:43:52,753 INFO  [Configuration] configuring from resource: /hibernate.cfg.xml
13:43:52,753 INFO  [Configuration] Configuration resource: /hibernate.cfg.xml
13:43:52,869 INFO  [Configuration] Reading mappings from resource : com/example/Zzz.hbm.xml
13:43:52,884 INFO  [Configuration] Configured SessionFactory: null
13:43:53,022 INFO  [HbmBinder] Mapping class: com.example.Zzz -> ZZZ
13:43:53,124 INFO  [DriverManagerConnectionProvider] Using Hibernate built-in connection pool (not for production use!)
13:43:53,124 INFO  [DriverManagerConnectionProvider] Hibernate connection pool size: 20
13:43:53,124 INFO  [DriverManagerConnectionProvider] autocommit mode: false
13:43:53,168 INFO  [DriverManagerConnectionProvider] using driver: oracle.jdbc.OracleDriver at URL: jdbc:oracle:thin:@//my_host
13:43:53,168 INFO  [DriverManagerConnectionProvider] connection properties: {user=***, password=***}
13:43:53,739 INFO  [SettingsFactory] RDBMS: Oracle, version: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
13:43:53,739 INFO  [SettingsFactory] JDBC driver: Oracle JDBC driver, version: 11.2.0.2.0
13:43:53,754 INFO  [Dialect] Using dialect: org.hibernate.dialect.Oracle10gDialect
13:43:53,758 INFO  [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.CMTTransactionFactory
13:43:53,760 INFO  [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
13:43:53,761 INFO  [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
13:43:53,761 INFO  [SettingsFactory] Automatic flush during beforeCompletion(): disabled
13:43:53,761 INFO  [SettingsFactory] Automatic session close at end of transaction: disabled
13:43:53,762 INFO  [SettingsFactory] JDBC batch size: 15
13:43:53,762 INFO  [SettingsFactory] JDBC batch updates for versioned data: disabled
13:43:53,762 INFO  [SettingsFactory] Scrollable result sets: enabled
13:43:53,762 INFO  [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
13:43:53,762 INFO  [SettingsFactory] Connection release mode: auto
13:43:53,763 INFO  [SettingsFactory] Default batch fetch size: 1
13:43:53,763 INFO  [SettingsFactory] Generate SQL with comments: disabled
13:43:53,763 INFO  [SettingsFactory] Order SQL updates by primary key: disabled
13:43:53,763 INFO  [SettingsFactory] Order SQL inserts for batching: disabled
13:43:53,763 INFO  [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
13:43:53,765 INFO  [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
13:43:53,765 INFO  [SettingsFactory] Query language substitutions: {}
13:43:53,765 INFO  [SettingsFactory] JPA-QL strict compliance: disabled
13:43:53,765 INFO  [SettingsFactory] Second-level cache: enabled
13:43:53,765 INFO  [SettingsFactory] Query cache: disabled
13:43:53,765 INFO  [SettingsFactory] Cache provider: org.hibernate.cache.NoCacheProvider
13:43:53,765 INFO  [SettingsFactory] Optimize cache for minimal puts: disabled
13:43:53,765 INFO  [SettingsFactory] Structured second-level cache entries: disabled
13:43:53,769 INFO  [SettingsFactory] Echoing all SQL to stdout
13:43:53,769 INFO  [SettingsFactory] Statistics: disabled
13:43:53,769 INFO  [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
13:43:53,769 INFO  [SettingsFactory] Default entity-mode: pojo
13:43:53,769 INFO  [SettingsFactory] Named query checking : enabled
13:43:53,789 INFO  [SessionFactoryImpl] building session factory
13:43:54,121 INFO  [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
13:43:54,122 INFO  [NamingHelper] JNDI InitialContext properties:{}
13:43:54,174 INFO  [STDOUT] Hibernate: insert into ZZZ (a, i) values (?, ?)

Но в базе данных нет новой записи, даже когда я останавливаю jBoss.Где моя вина?Пожалуйста, помогите.

PS Когда я не использую CMT и пишу код вроде:

tx = session.beginTransaction();
session.persist(...);
tx.commit();

все работает нормально.

1 Ответ

1 голос
/ 25 октября 2011

Я нашел решение: я переместил конфигурацию соединения с базой данных из hibernate.cfg.xml в источник данных (Z-ds.xml), и теперь он работает. Конечно, это не нормальное решение проблемы, а обходной путь. Спасибо всем!

...