How do I get the generator Id before saving to database?

How to get generator id before save to database?I had search a lot,but it’s can resolve my problem. My probject is use spring mvc and hibernate and mysql.

import javax.persistence.*;

@Table(name = "Test")
public class Test {
    private Integer id;
    private String name;

    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.TABLE,generator = "Test_gen")
    @TableGenerator(name = "Test_gen",table = "SerialNum",pkColumnName = 
    "tableName",valueColumnName = "tableValue",pkColumnValue = "Test")
    public Integer getId() {
       return id;

    public void setId(Integer id) { = id;

    public String getName() {
       return name;

    public void setName(String name) { = name;

Or how to use hibernate generator in other way? Like this:

public static long getGeneratorId(String entityName){
      return Hibernate.generatorId();

Here one of the solution to get the current table Identity, because you’re already using Hibernate you can use this line of code :

 session.CreateSqlQuery( "SELECT `AUTO_INCREMENT` " +
 "AND   TABLE_NAME   = 'Test' ").uniqueResult();