I have a 'customer' table in my postgresql database with a particular field: cus_number which has a sequence defined. This field is not the primary key, there's already an cus_id field.
The default value of the cus_number is
nextval('customer_cus_number_seq'::regclass) it's the sequence.
With pgadmin, when I insert a row in this customer table with a null value as the cus_number it works fine and the default value sequence is used.
But in my webap, when I persist a new Customer(), the row inserted has nothing in the field cus_number.
Here is my custom entity definition :
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "pers_id")
private Integer id;
...
@Column(name = "cus_number")
private Integer number;
...
}
And the sequence script :
CREATE SEQUENCE customer_cus_number_seq
INCREMENT 1
MINVALUE 100
MAXVALUE 9223372036854775807
START 114
CACHE 1;
ALTER TABLE customer_cus_number_seq OWNER TO fizzconsulting;
Do you some tips for me please ?
Thanks.
NULLvalue, aNULLvalue will be stored. The default clause is only used when the column is not mentioned in the column list of theinsertstatement (that's something different than supplying aNULLvalue)GenerationType.IDENTITYis for a different purpose (for an auto-increment column). You need a@SequenceGeneratorsomething along the lines above the fieldprivate Integer id;@SequenceGenerator(name = "customerIdSequence", sequenceName = "customer_cus_number_seq", allocationSize=1, initialValue=1)and then refer to this sequence using@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customerIdSequence"). Disclaimer to the statement : I do not use PostgreSQL but it should conceptually be the same as Oracle which I had been using in the past.DEFAULTkeyword is provided, but few apps and tools seem to support that :-(