javax / transaction / SystemException错误

时间:2016-02-25 12:11:49

标签: java hibernate maven jpa

我的项目有些问题,我想配置它,但是我收到了错误:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:61)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:162)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:61)
at com.politechnika.hibernatealone.application.HibernateAlone.main(HibernateAlone.java:23)

这是我的HibernateAlone.java文件:

import com.politechnika.hibernatealone.entity.Company;
import com.politechnika.hibernatealone.entity.Employee;
import com.politechnika.hibernatealone.entity.Position;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.Arrays;

public class HibernateAlone {
    public static void main(String[] args) {

        EntityManagerFactory factory = Persistence.createEntityManagerFactory("hibernateAlone");
        EntityManager manager = factory.createEntityManager();

        manager.getTransaction().begin();

        Position position = new Position();
        position.setPositionName("Manager");

        manager.persist(position);

        Company company = new Company();
        company.setCompanyName("Fulda");

        manager.persist(company);

        Employee employee = new Employee();
        employee.setPosition(position);
        employee.setFullName("Zenek Zenkowski");
        employee.setStreet("Paderewskiego");
        employee.setStreetNumber("26/64");
        employee.setCompany(company);

        manager.persist(employee);
        manager.getTransaction().commit();

        CriteriaBuilder criteriaBuilder = manager.getCriteriaBuilder();
        CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
        Root<Employee> from = query.from(Employee.class);
        CriteriaQuery<Employee> select = query.select(from);
        TypedQuery<Employee> queryFromManager = manager.createQuery(select);

        // manager.getTransaction().begin();

    for (Employee emp : queryFromManager.getResultList()) {
        System.out.println(emp.toString());
        // manager.remove(emp);
    }

    // manager.getTransaction().commit();
    manager.close();
    factory.close();
    }
}

这是配置文件persistence.xml

<persistence-unit name="hibernateAlone">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.politechnika.hibernatealone.entity.Employee</class>
    <class>com.politechnika.hibernatealone.entity.Company</class>
    <class>com.politechnika.hibernatealone.entity.Position</class>
    <!-- the JNDI data source -->
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.username" value="postgres"/>
        <property name="hibernate.connection.password" value="postgres"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>

        <property name="hibernate.default_schema" value="crm"/>

        <property name="hibernate.hbm2ddl.auto" value="create"/>

        <property name="show_sql" value="true"/>
    </properties>
</persistence-unit>

感谢您对此问题的帮助

1 个答案:

答案 0 :(得分:0)

您需要添加此依赖项才能使Hibernate正常工作。

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
    <version>1.1</version>
</dependency>

并且还使用开始和回滚事务取消注释代码部分,EntityManager需要使用事务内的实体。