引起:org.hibernate.exception.SQLGrammarException:无法执行语句

时间:2017-03-12 05:38:50

标签: java spring hibernate maven

大家好,我写第一个hibernate代码并有一些问题请帮忙。显示代码和错误

输入错误

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.tdapps.play_video.MainActivity"
android:background="?android:attr/actionModeSplitBackground">
 <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@android:color/black"
        android:layout_weight="1"
        android:id="@+id/imageView" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imgoverlay">

            <Button
                android:text="Import Image"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/Importimage"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="18sp"
                android:textStyle="bold"
                android:background="#00ffff"
                android:textColor="#000000"
                style="?android:attr/borderlessButtonStyle"
                />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/vignette">

            <SeekBar
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/vseekbar"
                android:layout_weight="1" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/color"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <Button
                android:id="@+id/Grayscale"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ffff"
                android:text="Grayscale"
                android:textAllCaps="false"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/Negative"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ffff"
                android:text="Negative"
                android:textAllCaps="false"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />

        </LinearLayout>






        <HorizontalScrollView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:background="#000000"
            android:id="@+id/blurview">

            <LinearLayout
                android:id="@+id/filter"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/BilateralFilter"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="#00ffff"
                    android:text="Bilateral"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/median"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="#00ffff"
                    android:text="Median"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/blur1"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00ffff"
                    android:text="Blur Level 1"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold"


                    />

                <Button
                    android:id="@+id/blur2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00ffff"
                    android:text="Blur Level 2"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold"


                    />

                <Button
                    android:id="@+id/blur3"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00ffff"
                    android:text="Blur Level 3"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/blur4"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00ffff"
                    android:text="Blur Level 4"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/blur5"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00ffff"
                    android:text="Blur Level 5"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/blur6"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00ffff"
                    android:text="Blur Level 6"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>
        </HorizontalScrollView>


        <LinearLayout
            android:id="@+id/light"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:id="@+id/Brightness"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:background="#00ffff"
                android:text="Brightness"
                android:textAllCaps="false"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/Contrast"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:background="#00ffff"
                android:text="Contrast"
                android:textAllCaps="false"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />

            <SeekBar
                android:id="@+id/seekBar2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00ffff"
                android:max="255" />
        </LinearLayout>

        <HorizontalScrollView

            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:scrollbars="none">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/Lightning"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"

                    android:background="#00ffff"
                    android:text="Lightning"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/Filter"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:background="#00ffff"
                    android:text="Filter"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />


                <Button
                    android:id="@+id/Crop"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"

                    android:background="#00ffff"
                    android:text="Crop"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/Color"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"

                    android:background="#00ffff"
                    android:text="Color"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/Rotate"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"

                    android:background="#00ffff"
                    android:text="Rotate"
                    android:textAllCaps="false"

                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/Vignette"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"

                    android:background="#00ffff"
                    android:text="Vignette"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/ImgOverlay"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"

                    android:background="#00ffff"
                    android:text="Image Overlay"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/PutText"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:background="#00ffff"
                    android:text="Put Text"
                    android:textAllCaps="false"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </HorizontalScrollView>

    </RelativeLayout>

</LinearLayout>

班级员工

    Exception in thread "main" 2017-03-11 21:53:11 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ERROR: permission denied for relation employee
2017-03-11 21:53:11 [main] INFO  o.h.e.j.b.internal.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements
2017-03-11 21:53:11 [main] ERROR o.h.i.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:224)
    at org.springframework.orm.hibernate5.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:755)
    at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:594)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
    at ua.goit.java.jdbc.model.controllers.EmployeeController$$EnhancerBySpringCGLIB$$f052d1ce.creatEmploee(<generated>)
    at ua.goit.java.jdbc.Main.start(Main.java:19)
    at ua.goit.java.jdbc.Main.main(Main.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3003)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3503)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:586)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:460)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1428)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:484)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3190)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2404)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
    at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:582)
    ... 15 more
Caused by: org.postgresql.util.PSQLException: ERROR: permission denied for relation employee
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2412)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2125)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:297)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169)
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:136)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:410)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:205)
    ... 33 more

Process finished with exit code 1

类HEmployeeDao

import javax.persistence.*;

@Entity
@Table(name = "employee")
public class Employee {
    @Id
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @Column(name = "phone_number")
    private String phoneNumber;
    @Enumerated(EnumType.STRING)
    @Column(name = "position")
    private Position position;
    @Column(name = "salary")
    private Float salary;


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

    public void setName(String name) {
        this.name = name;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

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

    public void setPosition(Position position) {
        this.position = position;
    }

    public void setSalary(Float salary) {
        this.salary = salary;
    }
}

类EmployeeControler

    import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import ua.goit.java.jdbc.model.Employee;
import ua.goit.java.jdbc.model.EmployeeDao;

import java.util.List;

public class HEmployeeDao implements EmployeeDao{

    private SessionFactory sessionFactory;

     @Transactional
    public void save(Employee employee) {
        sessionFactory.getCurrentSession().save(employee);
    }

       public Employee load(Long id) {
        return null;
    }

    public List<Employee> findAll() {
        return null;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

class Main

import org.springframework.transaction.annotation.Transactional;
import ua.goit.java.jdbc.model.Employee;
import ua.goit.java.jdbc.model.Position;
import ua.goit.java.jdbc.model.hibernate.HEmployeeDao;

public class EmployeeController {
    private HEmployeeDao employeeDao;

    @Transactional
    public void creatEmploee(){
        Employee employee = new Employee();
        employee.setId(1L);
        employee.setName("Alex");
        employee.setSurname("Kruglov");
        employee.setPhoneNumber("032-567-84");
        employee.setPosition(Position.WAITER);
        employee.setSalary(1000.00F);

        employeeDao.save(employee);
    }

    public void setEmployeeDao(HEmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }
}

Maven pom.xml

&LT;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ua.goit.java.jdbc.model.controllers.EmployeeController;

public class Main {

    private EmployeeController employeeController;

    public static void main(String[] args) {
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aplication-context.xml", "hibernate-context.xml");
        Main main = applicationContext.getBean(Main.class);
        main.start();
    }

    private void start() {
        employeeController.creatEmploee();
    }

    public void setEmployeeController(EmployeeController employeeController) {
        this.employeeController = employeeController;
    }
}

aplication-context.xm

?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hibernatePractick</groupId>
    <artifactId>goit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.0.0.jre7</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
        <!-- 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.javax.persistence/hibernate-jpa-2.1-api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>




    </dependencies>


</project>

冬眠-context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

        <bean id="main" class="ua.goit.java.jdbc.Main">
            <property name="employeeController" ref="employeeController"/>
        </bean>

        <bean id="employeeController" class="ua.goit.java.jdbc.model.controllers.EmployeeController">
            <property name="employeeDao" ref="employeeDao"/>
        </bean>

        <bean id="employeeDao" class="ua.goit.java.jdbc.model.hibernate.HEmployeeDao">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
</beans>

jdbc.propetries

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <tx:annotation-driven transaction-manager="txManeger" proxy-target-class="true"/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>


    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="driverClass" value="${jdbc.driver.class}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="minPoolSize" value="${jdbc.min.connections}"/>
        <property name="maxPoolSize" value="${jdbc.max.connections}"/>
        <property name="acquireIncrement" value="${jdbc.acquire.increment}"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="ua.goit.java.jdbc.model"/>
        <property name="hibernateProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
                </value>
        </property>
    </bean>

    <bean id="txManeger" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

0 个答案:

没有答案