使用 Hibernate 注释保存对象?

发布于 2024-11-09 09:38:15 字数 2477 浏览 4 评论 0原文

我只是想问知道在使用 Hibernate 注释创建数据库后如何创建对象?

当我运行下面的代码时,它会创建包含对象的数据库,但是当我第二次运行时,它只会创建完全相同的数据库,并且不会添加任何新对象?怎么会?在使用注释创建数据库后,如何使用 .save 方法使用注释创建对象?或者说不能用注释来做到这一点?

提前致谢。

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();
    }

}

这是代码 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;
    }

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

予囚 2024-11-16 09:38:15

您可能需要向我们展示您如何编写和注释 PersonAdress(原文如此)对象。

如果您编写了“正确的”equals()hashcode() 实现(即不查看object),那么您的 save() 调用在第二次时将不会执行任何操作,因为您要求保存的对象已存在于数据库中。

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.

始终不够爱げ你 2024-11-16 09:38:15

刚刚将 hibernate.hbm2ddl.auto 的设置从创建更改为创建更新,现在没有问题了......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文