• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Auto increment a non primay key column using hibernate annotation

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi

i want to increment a non primary key column by 1 every time a s new row is inserted.

We can generate and increment the primary key using these annotations

@Id
@SequenceGenerator(name = "generator", sequenceName = "some_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "id", unique = true, nullable = false)
@NotNull
public Long getId() {
return this.id;
}

but the same does not work on some other column of the same table

@SequenceGenerator(name = "generator", sequenceName = "some_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "otherColumn", unique = true, nullable = false)
@NotNull
public Long getOtherColumn() {
return this.otherColumn;
}

---> this is not working; on saving the object Hibernate throws this excpetion - org.hibernate.PropertyValueException: not-null property references a null or transient value: ...which mean the no value was generated for other column.

I m stuck with this. Please advise a solution.

Thanks!


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic