aspose file tools*
The moose likes Object Relational Mapping and the fly likes Auto increment a non primay key column using hibernate annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Auto increment a non primay key column using hibernate annotation" Watch "Auto increment a non primay key column using hibernate annotation" New topic
Author

Auto increment a non primay key column using hibernate annotation

Sandeep Kumar.
Greenhorn

Joined: Jul 23, 2008
Posts: 9

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!



SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Auto increment a non primay key column using hibernate annotation