猿问

如何在postgersql中自动增加id?

问候..我正在尝试将对象添加到我的 PostgreSQL 数据库,一切正常,但是当我在数据库中添加第二个对象时,它只会覆盖第一个对象


学生班


@Entity

@Table(name = "student")

public class Student {


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(columnDefinition="serial")

    private int id;


    @Column(name="first_name")

    private String firstName;


    @Column(name="last_name")

    private String lastName;


    @Column(name="email")

    private String email;

public Student() {


    }


    public Student(String firstName, String lastName, String email) {

        this.firstName = firstName;

        this.lastName = lastName;

        this.email = email;

    }

休眠配置文件


<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


<hibernate-configuration>

    <session-factory>

        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>

        <property name="hibernate.connection.username">user1</property>

        <property name="hibernate.connection.password">test123</property>

        <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hb_student_tracker</property>


        <property name="hibernate.current_session_context_class">thread</property>


        <property name="connection_pool_size">1</property>


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


        <property name="show_sql">true</property>


    </session-factory>

</hibernate-configuration>

第一个对象在数据库中写入正常,第二个对象将覆盖第一个对象



喵喔喔
浏览 190回答 1
1回答

慕无忌1623718

我认为您的问题是对 @Id 列使用原始类型。这是 Hibernate 在 javadoc 中对方法的说法save();保留给定的瞬态实例,首先分配生成的标识符。(或者,如果使用指定的生成器,则使用标识符属性的当前值。)如果关联是使用cascade=“save-update”映射的,则此操作会级联到关联的实例。CrudRepository#save()方法检查数据库中的学生 ID,第一条和第二条记录均为 0。由于 Hibernate 发现了一条记录并认为您没有插入一条记录,因此您正在更新一条 id 为 0 的记录。如果您确实像下面这样修改代码,它应该可以工作;@Entity@Table(name = "student")public class Student {  @Id  @GeneratedValue(strategy = GenerationType.IDENTITY)  @Column(columnDefinition="serial")  private Integer id;现在,Hibernate 将检查您的 id,如果它是一条新记录,则 id 字段将为空,因此 Hibernate 表示这是一条新记录,应该将其作为新记录插入。另外,还有一件事。您hbm2ddl.auto在休眠配置中进行设置。<property name="hbm2ddl.auto">create</property>在这种情况下,一旦CreatStudentDemo工作,休眠就会破坏你的旧表。这意味着,它删除 Student 表并创建一个新表。然后它按预期在数据库中创建一条学生记录,比方说 Student1。事情是这样的,您重新运行CreatStudentDemo,您期望第二个对象将作为 Student2 插入。然而,真正发生的事情是这样的——当您重新运行CreatStudentDemohibernate 时,会破坏 Student1 被删除的表。然后它创建 Student 表并插入 id 为 1 的 Student 对象,因为这是新创建的 Student 表上的第一条记录。基本上,你可以设置;<property name="hbm2ddl.auto">update</property>或者,您可以调用save()方法两次,例如;    Student student = new Student("Souid","Ayoub","ayoub@luv2code.com");        Student student2 = new Student("Test Student","TestStudent","test@mail.com");        //start the transaction        session.beginTransaction();        //save the object        System.out.println("Saving the Student...");        session.save(student);        session.save(student2);        .....
随时随地看视频慕课网APP

相关分类

Java
我要回答