简体   繁体   English

Hibernate:新的 Session 不掉表

[英]Hibernate: New Session without dropping the tables

I am new to Hibernate (implementing since yesterday) and i succesfully created a method, that transfers my Customer Objects to the Database.我是 Hibernate 的新手(从昨天开始实施),我成功地创建了一个方法,将我的客户对象传输到数据库。

After i quit my application and start it again and create a new session (in an other method) based on my hibernate.cfg.xml file with this setting:在我退出我的应用程序并再次启动它并使用此设置基于我的 hibernate.cfg.xml 文件创建一个新的 session (以其他方法)后:

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

It leads to that point, that all relevant tables, created with Hibernate are being deleted.它导致这一点,所有相关的表,用 Hibernate 创建的被删除。 So maybe that is a comprehension question, but i think "transparent persistence by hibernate" means also, that my POJO's are persistent beyond the runtime of my application??所以也许这是一个理解问题,但我认为“休眠的透明持久性”也意味着我的 POJO 在我的应用程序运行时之外是持久的?

So i read several topics on Stackoverflow and tried it with this setting:因此,我阅读了有关 Stackoverflow 的几个主题并使用此设置进行了尝试:

<property name="hibernate.hbm2ddl.auto">update</property>

But this leads to SQL errors:但这会导致 SQL 错误:

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'

Of course i don't want have duplicates, so i suppose that hibernate doesn't send a SQL Statement referring to an existing object.当然我不希望有重复,所以我想 hibernate 不会发送 SQL 语句引用现有的 object。

It sends a Statement like this:它发送如下声明:

UPDATE `customer` SET `id`=1,`birthday`='1990-10-05 00:00:00',`forename`='TestCustomer',`gender`='F',`generatedProfitsLastYear`='0',`generatedProfitsTotal`='0',`surename`='A',`gcid`='1' 

But i need the same statement, with a但我需要同样的声明,带有

Where `id`=1

at the end.在最后。

So basically what i want is, that hibernate doesn't drop all the tables and creates it again when i restart my application and create a new session based on the configuration file.所以基本上我想要的是,当我重新启动我的应用程序并根据配置文件创建一个新的 session 时,hibernate 不会删除所有表并再次创建它。 So after i open a new session, i can transfer the Customer Objects stored in the database to POJOs.因此,在我打开一个新的 session 后,我可以将存储在数据库中的客户对象传输到 POJO。 Did i understand the concept of hibernate incorrectly or am i making a typical beginners mistake?我是否错误地理解了 hibernate 的概念,还是我犯了一个典型的初学者错误?

Below you will find my Customer Class:您将在下面找到我的客户 Class:

@Entity
@Table(name="CUSTOMER")

public class Customer {
    private int id;
    private String forename;
    private String surname;
    private char gender;
    private Date birthday;
    private double generatedProfitsTotal;
    private double generatedProfitsLastYear;    
    private CustomerGroup assignedTo;

    public Customer(int id, String forename, String surname, char gender,
            Date birthday) {
        super();
        this.id = id;
        this.forename = forename;
        this.surname = surname;
        this.gender = gender;
        this.birthday = birthday;
    }

    @Id
    public int getId() {
        return id;
    }

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

    @Column(name = "forename")
    public String getForename() {
        return forename;
    }

    public void setForename(String forename) {
        this.forename = forename;
    }
    @Column(name = "surename")
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
    @Column(name = "gender")
    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
    @Column(name = "birthday")
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Column(name = "generatedProfitsTotal")
    public double getGeneratedProfitsTotal() {
        return generatedProfitsTotal;
    }

    public void setGeneratedProfitsTotal(double generatedProfitsTotal) {
        this.generatedProfitsTotal = generatedProfitsTotal;
    }
    @Column(name = "generatedProfitsLastYear")
    public double getGeneratedProfitsLastYear() {
        return generatedProfitsLastYear;
    }

    public void setGeneratedProfitsLastYear(double generatedProfitsLastYear) {
        this.generatedProfitsLastYear = generatedProfitsLastYear;
    }


    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="gcid", nullable=true, insertable=true, updatable=true)
    public CustomerGroup getAssignedTo() {
        return assignedTo;
    }

    public void setAssignedTo(CustomerGroup assignedTo) {
        this.assignedTo = assignedTo;
    }
}

my hibernate config file:我的 hibernate 配置文件:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/hibernatetesting</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping class="studyproject.Customer"/>
         <mapping class="studyproject.CustomerGroup"/>
         <mapping class="studyproject.BonusPackage"/>

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

Thanks谢谢

What did you do where the 'duplicate error' occurs? 在发生“重复错误”的地方您做了什么? Now I have the hibernate.hbm2ddl.auto configured as yours, but it's okay saving or updating entity in my local. 现在,我将hibernate.hbm2ddl.auto配置为您的,但是可以在本地保存或更新实体。

try session.saveOrUpdate() method where you have used session.save() it will prevent your database from dropping while fetching data use it with hbm2ddl.auto update it worked for me hope it helps.尝试使用 session.save() 方法的 session.saveOrUpdate() 方法,它将防止您的数据库在获取数据时下降,将其与 hbm2ddl.auto 一起使用更新它对我有用,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM