-1

I want to know, is there any "Property attribute" in hibernate to restrict duplicates while retrieving/inserting values to DB? I search a lot but everywhere I got - to use set in POJO.

Vickal
  • 161
  • 1
  • 8

1 Answers1

-1

You can mark a property unique by adding unique=true to its @Column annotation.

@Column(name = "MY_PROPERTY", unique = true)

Here is a simple example:

@Entity
@Access(AccessType.FIELD)
public class User extends AbstractEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "USERNAME", unique = true)
private String username;

// Getter & Setters
}
bhdrkn
  • 6,244
  • 5
  • 35
  • 42
  • 1
    Thank you ! I think i'm comfortable with that. – Vickal Jun 17 '15 at 06:17
  • This is only applied when using tools that generate/update db schema. See this [question](http://stackoverflow.com/questions/4546131/using-unique-constraint-on-hibernate-jpa2). If this is actually what the OP wants, then it should be stated in the answer. – Dragan Bozanovic Jun 17 '15 at 10:01
  • @DraganBozanovic I am well aware of this. But thanks for mentioning it. Unlike you, I understand that there is no limitation about how to restrict duplicates. Is it restricts duplicate by altering db schema? Yes its is. But it still restricts and approved by the owner. So, Is it occur to you that you might understand the question wrong? – bhdrkn Jun 17 '15 at 10:23