3

in java spring hibernate maven project, i use (@Id and @GeneratedValue(strategy = GenerationType.AUTO)) together on a field to make this field P.K and Auto-increment on mysql DB.

i want:

  • this field to be auto_increment if i did not set its id manually , and if i set it manually by setter , it works fine(but currently i get error if i set manually).

problem:

  • when i left the id null , auto_increment works , but when i set it manually i get error; how i annotate and config the model class that i can set manually by setter and automatic pk Id (anyone i like to do)?

my code:

public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id")
private Integer id; 

Annotation Detail based on java doc:

@Id : Specifies the primary key of an entity

@GeneratedValue(strategy = ... ) : Provides for the specification of generation strategies for the values of primary keys

  • GenerationType.IDENTITY : Indicates that the persistence provider must assign primary keys for the entity using a database identity column.

  • GenerationType.SEQUENCE : Indicates that the persistence provider must assign primary keys for the entity using a database sequence.

  • GenerationType.TABLE : Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.

  • GenerationType.AUTO : If you use AUTO, Hibernate will choose one of the strategies to generate your id

1 Answers1

0

You can define your custom id generator for this purpose as pointed out here and here

Here is how its code will look like:-

@Id
@Basic(optional = false)
@GeneratedValue(strategy=GenerationType.IDENTITY, generator="IdOrGenerated")
@GenericGenerator(name="IdOrGenerated",strategy="....UseIdOrGenerate")
@Column(name = "ID", unique = true, nullable = false, precision = 20, scale = 0)
public Long getId(){..}

and

public class UseIdOrGenerate extends IdentityGenerator {    
    @Override
    public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
        if (obj == null) throw new HibernateException(new NullPointerException()) ;

        if ((((EntityWithId) obj).getId()) == null) {//id is null it means generate ID
            Serializable id = super.generate(session, obj) ;
            return id;
        } else {
            return ((EntityWithId) obj).getId();//id is not null so using assigned id.

        }
    }
}
Community
  • 1
  • 1
Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113
  • a query: why do you have preceding 4 dots in strategy="....UseIdOrGenerate" . What is it meant for? – Sid Sep 26 '16 at 07:51
  • Oh that's just to generically state a strategy. You would ideally have a name of a concrete strategy there. – Andy Dufresne Sep 27 '16 at 06:26
  • Thanks for clarifying. Would have been more ideal to have the correct syntax over there for noobs like me out there in SO who look around to learn or understand. :) – Sid Sep 27 '16 at 10:11