@Entity(name = "USERS") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id; @Column(unique = true, nullable = false, name = "login") private String login; @Column(nullable = false, name = "name") private String name; @Column(nullable = false, name = "sur_name") private String surName; @Column(nullable = false, length = 32, name = "password") @Size(max = 32, min = 32) private String password; @Enumerated(EnumType.STRING) @Column(name = "position", nullable = false) private Position position = Position.CUSTOMER; @Column(nullable = false, name = "email") private String email; @Column(name = "salary", nullable = false) private double salary = 0.0; @Size(min = 0, max = 100) @Column(name = "prepayment") private short prepayment = 100; @OneToMany(mappedBy = "user") private List<Order> orders; public User() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurName() { return surName; } public void setSurName(String surName) { this.surName = surName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Position getPosition() { return position; } public void setPosition(Position position) { this.position = position; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public short getPrepayment() { return prepayment; } public void setPrepayment(short prepayment) { this.prepayment = prepayment; } public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } 

}

Code execution

 public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserDao ud = (UserDao)ctx.getBean("userDaoImpl"); User u = new User(); u.setName("Alex"); u.setLogin("mooks"); u.setSurName("Pdln"); u.setEmail("a@mail.com"); u.setPassword("password"); u.setPosition(Position.CUSTOMER); ud.addUser(u); u = new User(); u.setName("Alex"); u.setLogin("mooks"); u.setSurName("Pdln"); u.setEmail("a@mail.com"); u.setPassword("password"); u.setPosition(Position.CUSTOMER); ud.addUser(u); } 

All exactly in the database is recorded 2 identical entries. Tell me what could be the problem?

I use Spring + JPA (EclipseLink)

    2 answers 2

    If you did not create a table using JPA - you need to manually create a unique constraint. In more detail here and here

      The uniqueness of the written values ​​is guaranteed at the database level, specifying the unique annotation tells JPA that it is necessary to include this restriction in the DDL generated database schema and only.

      Accordingly, it is necessary to manually configure the table you created by adding the unique constraint :

       ALTER TABLE USERS ADD CONSTRAINT login_unq_constraint UNIQUE (login);