如何使用Maven在Intelij中连接Hibernate和Postgres?

时间:2017-12-29 14:26:31

标签: java spring postgresql hibernate maven

我在windows7x64上安装了Postgres 10.1版。 我有一个数据库“mydb”,其中一个表为“User”,列为“firstname”和“secondname”。 我正在使用Intelij和Maven。 下面带有内容的文件:

App.java

    tell application "Google Chrome" to tell active tab of window 1 to ¬
        execute javascript "document.getElementById('lst-ib').value"

    set my_field to result
    set the clipboard to my_field

HibernateUtil.java

package com.hibernateTest;

import org.hibernate.Session;

public class App {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        User user = new User();

        user.setFirstname("David");
        user.setLastname("Sol");

        session.save(user);
        session.getTransaction().commit();
    }
}

用户:

package com.hibernateTest;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }

}

的hibernate.cfg.xml

package com.hibernateTest;

import javax.persistence.Column;
import javax.persistence.Table;

@Table(name = "User")
public class User implements java.io.Serializable {

    private String firstname;
    private String lastname;

    public User() {
    }

    public User(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    @Column(name = "firstname", nullable = false, length = 20)
    public String getFirstname() {
        return this.firstname;
    }

    public void setFirstname(String username) {
        this.firstname = firstname;
    }

    @Column(name = "lastname", nullable = false, length = 20)
    public String getLastname() {
        return this.lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}

的pom.xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mydb</property>
        <property name="connection_pool_size">1</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
        <mapping class="com.hibernateTest.User"/>
    </session-factory>
</hibernate-configuration>

正确知道当我尝试运行我的应用程序时出现错误:“线程中的异常”主“org.hibernate.MappingException:未知实体:com.hibernateTest.User”。因此maping或配置存在问题。 Hibernate没有看到表用户或登录名或密码不正确?请问你能帮帮我吗。非常感谢你。

0 个答案:

没有答案