@Column(unique = true)



  • @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);
    

    }

    Everyone on the bd record two identical records.
    What's the problem?

    Used Spring + JPA(EclipseLink)



  • If you didn't set up a table with JPA, you need to manually create a unique line. Further details https://stackoverflow.com/a/3498242/2546083 and https://stackoverflow.com/q/15372654/2546083


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2