1

Бьюсь с проблемой второй день - результаты каждый раз разные, но итог отрицательный. Делаю этот пример на IDEA 13.1.4, использую org.hibernate:hibernate-entitymanager:4.3.6.Final и postgresql:postgresql:9.2-1002.jdbc4 и JPA 2.1-2.1

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:postgresql://127.0.0.1:5432/Overhaul</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">postgres</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="connection.pool_size">1</property>
        <mapping class="models.Question"/>
        <mapping resource="Question.hbm.xml"/>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

Question.hbm.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="models">
    <class name="Question" table="Question">
        <id name="id" column="id">
            <generator class="sequence"/>
        </id>
        <property name="message" type="java.lang.String" column="message"/>
        <property name="authorName" type="java.lang.String" column="author_name"/>
        <property name="postDate" type="java.util.Date" column="post_date"/>
    </class>
</hibernate-mapping>

Question.java(сгенерировала сама IDEA):

package models;

import javax.persistence.*;
import java.sql.Date;

@Entity
@Table(name = "Question", schema ="public", catalog = "Overhaul")
public class Question {
    private int id;
    private String message;
    private String authorName;
    private Date postDate;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

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

    @Basic
    @Column(name = "message")
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Basic
    @Column(name = "author_name")
    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    @Basic
    @Column(name = "post_date")
    public Date getPostDate() {
        return postDate;
    }

    public void setPostDate(Date postDate) {
        this.postDate = postDate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Question question = (Question) o;

        if (id != question.id) return false;
        if (authorName != null ? !authorName.equals(question.authorName) : question.authorName != null) return false;
        if (message != null ? !message.equals(question.message) : question.message != null) return false;
        if (postDate != null ? !postDate.equals(question.postDate) : question.postDate != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (message != null ? message.hashCode() : 0);
        result = 31 * result + (authorName != null ? authorName.hashCode() : 0);
        result = 31 * result + (postDate != null ? postDate.hashCode() : 0);
        return result;
    }
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
    <persistence-unit name="NewPersistenceUnit">
        <!--<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>-->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>models.Question</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/Overhaul"/>
            <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.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

Почему при выполнении from Question в Hibernate Console у меня " 0 rows retrieved"? Записи в таблице минимум две!

1 ответ 1

1

Я бы на Вашем месте проверил вот это

<property name="connection.url">jdbc:postgresql://127.0.0.1:5432/Overhaul</property>

и это

@Table(name = "Question", schema ="public", catalog = "Overhaul")

Кстати, аннотации в файле Question.java дублируют файл Question.hbm.xml.

1
  • А что там проверять? datasource отлично цепляется по этому url-у. Поменял postgresql на mysql (т.е. субд) - все заработало как часы.... не могу понять, в чем косяк.... 18 сен 2014 в 18:32

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.