简体   繁体   中英

Hibernate get ID from composite PK (sequence in DB) after insert

How I can get ID generated by sequence in database, after insert record?

Hibernate config:

<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">org.firebirdsql.jdbc.FBDriver</property>
  <property name="hibernate.connection.password">masterkey</property>
  <property name="hibernate.connection.url">jdbc:firebirdsql:localhost/3050:FB3</property>
  <property name="hibernate.connection.username">SYSDBA</property>
  <property name="hibernate.dialect">org.hibernate.dialect.FirebirdDialect</property>
  <property name="hibernate.connection.autocommit">false</property>
  <property name="hibernate.connection.isolation">2</property>
  <property name="hibernate.connection.release_mode">on_close</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.use_sql_comments">true</property>
  <property name="hibernate.default_entity_mode">pojo</property>
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="hibernate.generate_statistics">false</property>
  <property name="hibernate.hbm2ddl.auto">validate</property>
  <property name="hibernate.transaction.auto_close_session">false</property>
  <property name="hibernate.connection.charSet">UTF-8</property>
  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">10</property>
  <property name="hibernate.c3p0.timeout">5</property>
  <property name="hibernate.c3p0.idle_test_period">2</property>
  <property name="hibernate.c3p0.acquire_increment">1</property>
  <property name="javax.persistence.validation.mode">none</property>
  <mapping class="com.zvpblog.OraHib.model.fb.entity.MyTable"/>
  <mapping class="com.zvpblog.OraHib.model.fb.entity.Usr"/>
 </session-factory>
</hibernate-configuration>

Usr entity:

@Entity
@Table(name = "USR")
public class Usr {
    @EmbeddedId
    private UsrPK id;

    @Column(name = "FNAME", columnDefinition = "VARCHAR")
    private String firstName;

    @Column(name = "DTMSTMP", columnDefinition = "TIMESTAMP")
    private Timestamp dTmStmp;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "MYTABLEID")
    private MyTable myTable;

    public Usr() {}

    public Usr(String login, String firstName) {
       this.setId(new UsrPK(null, login));
       this.setFirstName(firstName);
    }

    @Override
    public String toString() {
       Object[] args = { this.id.getId(), this.id.getLogin(), this.getFirstName(), this.getMyTable().getId(), getdTmStmp() };
       MessageFormat text = new MessageFormat("Object: {0} [id={1}, login={2}, firstName={3}, myTableId={4}, dTmStmp={5}");
       return text.format(args);
    }

    @Embeddable
    public static class UsrPK implements Serializable {
       private static final long serialVersionUID = 1L;

       @Column(name = "ID", columnDefinition = "BIGINT", unique = true)
       @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PK")
       @SequenceGenerator(name = "PK", sequenceName = "GEN_USR_ID", allocationSize = 1)
       private Long id;

       @Column(name = "LOGIN")
       private String login;

       public UsrPK() {}

       public UsrPK(Long id, String login) {
          this.setId(id);
           this.setLogin(login);
       }

       public Long getId() {
          return id;
       }

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

       public String getLogin() {
          return login;
       }

       public void setLogin(String login) {
          this.login = login;
       }

    }

    public UsrPK getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public Timestamp getdTmStmp() {
        return dTmStmp;
    }

    public void setdTmStmp(Timestamp dTmStmp) {
        this.dTmStmp = dTmStmp;
    }

    public MyTable getMyTable() {
        return myTable;
    }

    public void setMyTable(MyTable myTable) {
        this.myTable = myTable;
    }

} 

I try this code, but unsuccessfully (getting NULL for id ):

session = HibernateUtil.getSession();
session.beginTransaction();
System.out.println("Before insert ->>> " + usr.getId() + " | " + usr.getLogin() + " <<<-");
Usr.UsrPK test = (Usr.UsrPK) session.save(usr);
session.getTransaction().commit();
System.out.println("After insert ->>> " + test.getId() + " | " + test.getLogin() + " <<<-");

Console output:

Before insert ->>> null | login <<<-

Hibernate: /* insert com.zvpblog.OraHib.model.fb.entity.Usr */ insert into USR (DTMSTMP, FNAME, MYTABLEID, ID, LOGIN) values (?, ?, ?, ?, ?)

After insert ->>> null | login <<<-

Change your @EmbeddedId in Usr class with below

@EmbeddedId
private UsrPK id;

Add to UsrPK id variable the annotations

@Column(name = "ID", columnDefinition = "BIGINT", unique = true)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PK")
@SequenceGenerator(name = "PK", sequenceName = "GEN_USR_ID", allocationSize = 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM