我对此有点不安,我正在寻求帮助,只是为了了解发生了什么。
所以,我有这个表(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。
但在数据库中,我认为一切都正确。
当我搜索所有用户时,我会收到以下信息:
public List<User> getAll() {
return userRepository.findAll();
}
相同的JPDA ObjectReference。
欢迎任何帮助。
感谢。
答案 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.
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.