0

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

Neil Stockton
  • 10,922
  • 3
  • 30
  • 28
UDE_Student
  • 327
  • 1
  • 2
  • 18
  • please, have a look: http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do – melli-182 Jul 06 '15 at 18:48
  • Thanks, maybe i don't understand it, but when i am using update i am on the right track, right? Unfortunately "Update" leads to a wrong SQL Statement, wihout an WHERE id=X at the end. So that leads to that "duplicate" error. – UDE_Student Jul 06 '15 at 18:55
  • i have edited my starting post with the statement that Hibernate sends and what i want that hibernate should send. – UDE_Student Jul 06 '15 at 19:00
  • did you try using the property with the value "validate"? – melli-182 Jul 06 '15 at 19:01
  • i found a solution, before i used session.persist(Customer), i had to use session.saveOrUpdate(Customer) – UDE_Student Jul 06 '15 at 19:12
  • But i have the problem, if i reopen a new session and try to get all the Customer objects in a List, i get NULL. allCustomers=ArrayList) session.createQuery("FROM Customer").list(); ----- – UDE_Student Jul 06 '15 at 19:20

1 Answers1

0

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.

Sky
  • 6,581
  • 8
  • 28
  • 40