Search code examples
javahibernatehibernate-mapping

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.

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:

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

It leads to that point, that all relevant tables, created with Hibernate are being deleted. 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!?

So i read several topics on Stackoverflow and tried it with this setting:

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

But this leads to SQL errors:

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.

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. So after i open a new session, i can transfer the Customer Objects stored in the database to POJOs. Did i understand the concept of hibernate incorrectly or am i making a typical beginners mistake?

Below you will find my Customer 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-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


Solution

  • 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.