我无法生成自动递增的Id和hibernate总是生成1作为Id

时间:2017-12-18 04:09:09

标签: mysql spring hibernate spring-boot spring-data

我对此有点不安,我正在寻求帮助,只是为了了解发生了什么。

所以,我有这个表(DDL):

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(45) NOT NULL,
  `lastname` varchar(45) NOT NULL,
  `email` varchar(60) NOT NULL,
  `password` varchar(100) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `image_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email_UNIQUE` (`email`),
  KEY `FK17herqt2to4hyl5q5r5ogbxk9` (`image_id`),
  CONSTRAINT `FK17herqt2to4hyl5q5r5ogbxk9` FOREIGN KEY (`image_id`) REFERENCES    `images` (`id`),
CONSTRAINT `fk_users_images1` FOREIGN KEY (`image_id`) REFERENCES `images` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

我的实体:

@Entity
@Table(name = "users")
public class User implements Serializable {

    private static final long serialVersionUID = 6304869652591140818L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column(name = "firstname")
    private String firstname;

    @Column(name = "lastname")
    private String lastname;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    @Transient
    private String password;

   @Column(name = "enabled")
   private int enabled;

   @Column(name = "created_at")
   private LocalDateTime createdAt;

   @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinTable(name = "users_has_roles",
        joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id"))
   private Set<Role> roles;

   @MapsId
   @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   private Image image;

   // Setters and Getters
}

我正在尝试使用此代码保留新用户:

@PersistenceContext
private EntityManager em;

@Transactional
public void createUser(NewUserDto newUser) {

    User user = new User();
    user.setFirstname(newUser.getFirstname());
    user.setLastname(newUser.getLastname());
    user.setEmail(newUser.getEmail());
    user.setEnabled(ENABLED);
    user.setPassword(bCryptPasswordEncoder.encode(newUser.getPassword()));

    Image userImage = imageService.getImageById(1L);
    user.setProfileImage(userImage);


    Set<Role> roles = new HashSet<Role>();

    Role role = roleRepository.findByName(newUser.getRole());
    roles.add(role);

    user.setRoles(roles);

    if (user.getId() == null) {
        em.persist(user);
        em.flush();
    } else {
        em.merge(user);
    }

}

问题是,当我调试时,我发现总是为新用户生成id = 1。

但在数据库中,我认为一切都正确。

enter image description here

当我搜索所有用户时,我会收到以下信息:

public List<User> getAll() {
    return userRepository.findAll();
}

enter image description here

相同的JPDA ObjectReference。

欢迎任何帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

(Apologies for not having enough reputation to leave a comment)

I do not understand your question clearly. Anyway i will try to explain based on what I think is your problem.

  • By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. So that behavior is expected.
  • If you want it to start with a value that is not 1, then you must use a GenerationType.SEQUENCE with appropriate changes.
  • Image1: You have mentioned that the values are getting entered into the database correctly and this shows that the Id values are being generated correctly
  • Image2: users is a Javascript array. Index values of javascript arrays always start from zero. That is just the index value and NOT the id value. Similarly, the id mentioned on the right is the javascript's internal object id and not the actual Id of your user.
  • To see the real id of the user, expand the user at position 0.

If this does not answer your questions, kindly elaborate your question and what exactly is the error or issue.

答案 1 :(得分:1)

The problem stems from a misunderstanding where Hibernate's fallback for GenerationType.AUTO is GenerationType.SEQUENCE, while what most people want is GenerationType.IDENTITY, so you can use the id the database assigns you. simply change the strategy to IDENTITY and your problem is solved.

相关问题