首页 > 解决方案 > microsoft sql server 和 maven 的休眠问题

问题描述

我正在尝试将休眠 orm 映射工具配置到我的 java 类并使用 MS-SQL Server 作为我的数据库并将密码配置为“sa@123”。当我尝试运行应用程序时,我在控制台日志中遇到了错误,显示为无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]。

 29-Nov-2018 19:30:44.202 INFO [http-nio-8080-exec-41] org.hibernate.Version.logVersion HHH000412: Hibernate Core {5.2.8.Final}
 29-Nov-2018 19:30:44.202 INFO [http-nio-8080-exec-41] org.hibernate.cfg.Environment.<clinit> HHH000206: hibernate.properties not found
 29-Nov-2018 19:30:44.226 WARN [http-nio-8080-exec-41] org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver.resolveEntity           
HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
29-Nov-2018 19:30:44.304 WARN [http-nio-8080-exec-41] org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver.resolveEntity 
HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
29-Nov-2018 19:30:44.398 INFO [http-nio-8080-exec-41] org.hibernate.annotations.common.reflection.java.JavaReflectionManager.<clinit> HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
29-Nov-2018 19:30:45.914 INFO [http-nio-8080-exec-41] org.hibernate.c3p0.internal.C3P0ConnectionProvider.configure HHH010002: C3P0 using driver: com.microsoft.sqlserver.jdbc.SQLServerDriver at URL: jdbc:sqlserver://197.51.205.198:1433;databaseName=hayez
29-Nov-2018 19:30:45.914 INFO [http-nio-8080-exec-41] org.hibernate.c3p0.internal.C3P0ConnectionProvider.configure HHH10001001: Connection properties: {user=sa, password=****}
29-Nov-2018 19:30:45.914 INFO [http-nio-8080-exec-41] org.hibernate.c3p0.internal.C3P0ConnectionProvider.configure HHH10001003: Autocommit mode: false
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

以下是我的 hibernate.cfg.xml 文件:

  <hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://197.51.205.198:1433;databaseName=hayez</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">sa@123</property>

      <!-- JDBC connection pool (use the built-in) -->
        <!-- Use the C3P0 connection pool provider -->
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_periods">3000</property>       


        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>


   <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>



    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">1</property>


    <mapping resource="tech/basarsoft/hayez/io/entity/Branch.hbm.xml"/>




  </session-factory>
</hibernate-configuration>

这是我的 POJO 文件

@Entity
@Table(name="Branch"
    ,schema="dbo"
    ,catalog="hayez"
)
public class Branch  implements java.io.Serializable {

    private static final long serialVersionUID = 8996015700543450236L;

    @Id
    @GeneratedValue
    @Column(name="id", unique=true, nullable=false)
    private String id;
    @Column(name="code")
     private String code;
@Column(name="currentVersion")
     private Integer currentVersion;
@Temporal(TemporalType.TIMESTAMP)
     @Column(name="date", length=23)
     private Date date;
@Column(name="description")
     private String description;
@Column(name="remark1")
     private String remark1;
@Column(name="name1")
     private String name1;
@Column(name="name2")
     private String name2;
@Column(name="activated")
     private Boolean activated;
@Column(name="address")
     private String address;
@Column(name="phoneNumber")
     private String phoneNumber;
@Column(name="entityType")
     private String entityType;
@OneToMany(fetch=FetchType.LAZY, mappedBy="branch")
     private Set quantities = new HashSet(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="branch")
     private Set purchaseInvoices = new HashSet(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="branch")
     private Set employees = new HashSet(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="branch")
     private Set rooms = new HashSet(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="branch")
     private Set salesInvoices = new HashSet(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="branch")
     private Set warehouses = new HashSet(0);

    public Branch() {
    }


    public Branch(String id) {
        this.id = id;
    }
    public Branch(String id, String code, Integer currentVersion, Date date, String description, String remark1, String name1, String name2, Boolean activated, String address, String phoneNumber, String entityType, Set quantities, Set purchaseInvoices, Set employees, Set rooms, Set salesInvoices, Set warehouses) {
       this.id = id;
       this.code = code;
       this.currentVersion = currentVersion;
       this.date = date;
       this.description = description;
       this.remark1 = remark1;
       this.name1 = name1;
       this.name2 = name2;
       this.activated = activated;
       this.address = address;
       this.phoneNumber = phoneNumber;
       this.entityType = entityType;
       this.quantities = quantities;
       this.purchaseInvoices = purchaseInvoices;
       this.employees = employees;
       this.rooms = rooms;
       this.salesInvoices = salesInvoices;
       this.warehouses = warehouses;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }


    public Integer getCurrentVersion() {
        return this.currentVersion;
    }

    public void setCurrentVersion(Integer currentVersion) {
        this.currentVersion = currentVersion;
    }

    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public String getRemark1() {
        return this.remark1;
    }

    public void setRemark1(String remark1) {
        this.remark1 = remark1;
    }


    public String getName1() {
        return this.name1;
    }

    public void setName1(String name1) {
        this.name1 = name1;
    }


    public String getName2() {
        return this.name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }


    public Boolean getActivated() {
        return this.activated;
    }

    public void setActivated(Boolean activated) {
        this.activated = activated;
    }


    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


    public String getPhoneNumber() {
        return this.phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }


    public String getEntityType() {
        return this.entityType;
    }

    public void setEntityType(String entityType) {
        this.entityType = entityType;
    }

    public Set getQuantities() {
        return this.quantities;
    }

    public void setQuantities(Set quantities) {
        this.quantities = quantities;
    }

    public Set getPurchaseInvoices() {
        return this.purchaseInvoices;
    }

    public void setPurchaseInvoices(Set purchaseInvoices) {
        this.purchaseInvoices = purchaseInvoices;
    }

    public Set getEmployees() {
        return this.employees;
    }

    public void setEmployees(Set employees) {
        this.employees = employees;
    }

    public Set getRooms() {
        return this.rooms;
    }

    public void setRooms(Set rooms) {
        this.rooms = rooms;
    }

    public Set getSalesInvoices() {
        return this.salesInvoices;
    }

    public void setSalesInvoices(Set salesInvoices) {
        this.salesInvoices = salesInvoices;
    }

    public Set getWarehouses() {
        return this.warehouses;
    }

    public void setWarehouses(Set warehouses) {
        this.warehouses = warehouses;
    }

}

我的应用程序类:

public class BranchServiceImpl implements BranchService{
        BranchDAO database;

       public BranchServiceImpl(BranchDAO database) {
        this.database = database;
    }

    EntityUtils entityUtils = new EntityUtils();

    public BranchDTO createBranch(BranchDTO branch) {
 BranchDTO returnValue = null;


        String branchId = entityUtils.generateEntityId(30);
        branch.setId(branchId);
        System.err.println("t "+ branchId);


        // Record data into a database 
        returnValue = this.saveBranch(branch);


        // Return back the user profile
        return returnValue;

    }
 private BranchDTO saveBranch(BranchDTO branch) {
        BranchDTO returnValue = null;
        // Connect to database 

        try {
            this.database.openConnection();
            returnValue = this.database.saveBranch(branch);
        } finally {
            this.database.closeConnection();
        }

        return returnValue;
    }

这是我的休眠 Utils 类

public class HibernateUtils {
   private static final SessionFactory sessionFactory;

    static {
        Configuration conf = new Configuration();
        conf.configure();

        try {

//            sessionFactory = new Configuration().configure().buildSessionFactory();
            sessionFactory =
    new Configuration().configure().buildSessionFactory();

        } catch (HibernateException e) {
            System.err.println("Initial SessionFactory creation failed." + e);
            throw new ExceptionInInitializerError(e);
        }       
    }

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

这是 POM 依赖项

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.1.2.jre8-preview</version>
            <scope>test</scope>
        </dependency>

           <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -->
        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>2.26</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.26</version>
        </dependency>

        <!-- Hibernate Dependencies -->
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.8.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.0.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>5.2.12.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ses -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-ses</artifactId>
            <version>1.11.232</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.ext/jersey-spring4 -->
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring4</artifactId>
            <version>2.26</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

这是我的错误堆栈跟踪

at tech.basarsoft.hayez.service.impl.BranchServiceImpl.createBranch(BranchServiceImpl.java:58)
at tech.basarsoft.hayez.ui.entrypoint.BranchEntryPoint.createBranch(BranchEntryPoint.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$$Lambda$248.247754449.invoke
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:243)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

标签: javahibernatemavenjakarta-ee

解决方案


推荐阅读