Tôi nhận được ERROR: duplicate key value violates unique constraint "users_pkey" Detail: Key (userid)=(2701) already exists.
bất cứ khi nào tôi sử dụng mô hình cố định để cập nhật thực thể người dùng của mình.Cập nhật thực thể bằng EntityManager JPA EclipseLink
Trong mẫu mã bên dưới: SetLoginAttempts lấy một thực thể người dùng đã được truy vấn và khi tôi bắt đầu giao dịch, tôi chỉ cần đặt một trong các trường của thực thể và gọi persist(), sau đó cam kết giao dịch.
/**
* @param user
* @param attemptNumber
*/
@Transactional
public void setLoginAttempts(Users user, int attemptNumber){
user.setLoginAttempts(attemptNumber);
System.out.println(user);
}
Dưới đây là làm thế nào tôi tham khảo và lấy người quản lý thực thể:
eFactory = Persistence.createEntityManagerFactory("persistenceUnit");
eManager = eFactory.createEntityManager();
Khi nhìn vào stack trace, tôi nhận thấy rằng các cam kết thực sự tiêm nhiễm một chèn
Call: INSERT INTO USERS (userID, EMAIL, ISLOCKED, LOGINATTEMPTS, passwordHash, passwordSalt, USERNAME, version) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
bind => [2701, [email protected], false, 1, $shiro1$SHA-256$500000$6mqzZ/d/3BLQuJqLh1dDhQ==$NKW7Z++o/JTvf884aDWhP3Uhpyb5fTPMrm4joWnw7nI=, [[email protected], admin, 1]
là gì cách thích hợp để tham chiếu trình quản lý thực thể trong Spring roo, cập nhật trường và cam kết các thay đổi?
Sửa
tôi thêm @Transactional đến phương pháp và stack trace cho thấy rằng điều này đang tạo ra những trường hợp quản lý thực thể:
2012-03-14 23:49:15,503 ["http-bio-8080"-exec-18] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Creating new transaction with name [org.bixin.dugsi.service.UserService.setLoginAttempts]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-03-14 23:49:15,503 ["http-bio-8080"-exec-18] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Opened new EntityManager [[email protected]] for JPA transaction
[EL Finer]: 2012-03-14 23:49:15.503--ServerSession(2128384958)--Thread(Thread["http-bio-8080"-exec-18,5,main])--client acquired: 1116759395
[EL Finer]: 2012-03-14 23:49:15.503--ClientSession(1116759395)--Thread(Thread["http-bio-8080"-exec-18,5,main])--acquire unit of work: 368076985
2012-03-14 23:49:15,503 ["http-bio-8080"-exec-18] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Not exposing JPA transaction [[email protected]] as JDBC transaction because JpaDialect [[email protected]] does not support JDBC Connection retrieval
Email: [email protected], Id: 2701, IsLocked: false, LoginAttempts: 2, Password: $shiro1$SHA-256$500000$6mqzZ/d/3BLQuJqLh1dDhQ==$NKW7Z++o/JTvf884aDWhP3Uhpyb5fTPMrm4joWnw7nI=, PasswordSalt: [[email protected], Roles: 0, Username: admin, Version: null
2012-03-14 23:49:15,503 ["http-bio-8080"-exec-18] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Initiating transaction commit
2012-03-14 23:49:15,503 ["http-bio-8080"-exec-18] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Committing JPA transaction on EntityManager [[email protected]]
[EL Finer]: 2012-03-14 23:49:15.503--UnitOfWork(368076985)--Thread(Thread["http-bio-8080"-exec-18,5,main])--begin unit of work commit
[EL Finer]: 2012-03-14 23:49:15.503--UnitOfWork(368076985)--Thread(Thread["http-bio-8080"-exec-18,5,main])--end unit of work commit
[EL Finer]: 2012-03-14 23:49:15.504--UnitOfWork(368076985)--Thread(Thread["http-bio-8080"-exec-18,5,main])--resume unit of work
2012-03-14 23:49:15,504 ["http-bio-8080"-exec-18] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Closing JPA EntityManager [[email protected]] after transaction
2012-03-14 23:49:15,504 ["http-bio-8080"-exec-18] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
[EL Finer]: 2012-03-14 23:49:15.504--UnitOfWork(368076985)--Thread(Thread["http-bio-8080"-exec-18,5,main])--release unit of work
Nhưng vẫn không có bản cập nhật cho DB ngay cả sau khi làm mới , tại sao không phải là giao dịch đóng và cập nhật DB?
Tôi chỉ cố gắng thay thế tồn tại() với merge() và nó có vẻ như không có gì xảy ra. Nhìn vào dấu vết ngăn xếp một lần nữa và tôi nhận được điều này "Bắt đầu một cuộn giao dịch trở lại" vì một lý do nào đó? làm thế nào để tôi ngăn chặn nó quay trở lại vì vậy tôi có thể thấy những thay đổi trong DB – Warz
Viết sau đây trong lớp học: '@PersistenceContext (unitName =" myEntityManager ") thực thể EntityManager riêngMgr; public void setLoginAttempts (Người dùng người dùng, int tryNumber) { user.setLoginAttempts (tryNumber); entityMgr.merge (người dùng); } người dùng công cộng getUser (Object userId) { trả về thực thểMgr.find (User.class, userId); } ' – Ameya