This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Auto increment a non primay key column using hibernate annotation

 
Sandeep Kumar.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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!


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic