0

I just wanna ask know how its possible to create object after creating your database with Hibernate annotations?

When i run the code below, it creates the database with the objects, but when i run the second time it just creates exactly the same, and none new objects are added? How come? How do i create objects using annotations with the method .save, after creating the database with annotations? Or is it not possible to do so with annotations?

Thanks in advance.

public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();

        Adress adress = new Adress("Streetname", "postcode");

        Person person1 = new Person("Peter Hanks", adress);
        Person person2 = new Person("Sophie Hanks", adress);

        session.save(person1);
        session.save(person2);
        transaction.commit();



    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

}

Heres the code person.class

@Entity
@Table(name="person")
public class Person implements Serializable {
private long id;
private String navn;
private Adresse adresse;

public Person() {
}

public Person(String navn, Adresse adresse) {
    this.navn = navn;
    this.adresse = adresse;
}

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name= "adresse_id", nullable = false)
public Adresse getAdresse() {
    return adresse;
}

public void setAdresse(Adresse adresse) {
    this.adresse = adresse;
}

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

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

@Column(name = "navn", nullable= false, length= 100)
public String getNavn() {
    return navn;
}

public void setNavn(String navn) {
    this.navn = navn;
}


@Override
public int hashCode() {
    int hash = 3;
    hash = 29 * hash + (this.navn != null ? this.navn.hashCode() : 0);
    hash = 29 * hash + (this.adresse != null ? this.adresse.hashCode() : 0);
    return hash;
}





@Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if ((this.navn == null) ? (other.navn != null) : !this.navn.equals(other.navn)) {
            return false;
        }
        if (this.adresse != other.adresse && (this.adresse == null || !this.adresse.equals(other.adresse))) {
            return false;
        }
        return true;
    }
Newbie
  • 2,664
  • 7
  • 34
  • 75
Omer
  • 1
  • 2

2 Answers2

0

You might need to show us how you've written and annotated your Person and Adress (sic) objects.

If you've written "correct" equals() and hashcode() implementations (i.e. that don't look at the @Id of the object) then your save() calls will do nothing the second time around because the objects you've asked to save already exist in the database.

millhouse
  • 9,817
  • 4
  • 32
  • 40
  • I dont get it.. I am using @GeneratedValue, so it shouldnt be a problem. The database is counting up the id, so it cant be the same objects?? – Omer May 25 '11 at 00:38
  • Between running the first and second time, change "Peter" to "Paul" and "Sophie" to "Sarah". Do you get more objects now? – millhouse May 25 '11 at 00:48
  • no, running second time the first objects still stands in the database.. the second added are not recorded??? I just added the code for person.class – Omer May 25 '11 at 00:56
  • Odd. Code looks OK, how are you getting Hibernate to build your schema? In your question you say "it creates the database with the objects" - which hbm2ddl mode are you using? – millhouse May 25 '11 at 01:09
  • ok.. i am using create.. create – Omer May 25 '11 at 01:18
  • another thing i just realised, is that when i just change the adress, it just updates the name in the database??? – Omer May 25 '11 at 01:24
  • OK something strange is going on. Just to rule out some possible causes, can I suggest you add some `equals()` and `hashcode()` implementations to your objects that **do not** include the `id` field in their calculations. This has always been a good idea when dealing with persisted entities. – millhouse May 25 '11 at 01:39
  • I just tried with hashcode() and equals() and it didnt change anything.. i have added the code for the methods for person.class – Omer May 25 '11 at 01:57
  • I just found out another way to do let hibernate know classes i have annotated.. I added som code in my HibernateUtil.class, just added addAnnotatedClass(person.class) and know thereis no problem... But thx for you help anyway... – Omer May 25 '11 at 03:01
0

just changed the settings for hibernate.hbm2ddl.auto from create to create-update, and now theres no problem...

Omer
  • 1
  • 2