代码之家  ›  专栏  ›  技术社区  ›  Jignesh M. Khatri

JPA CrudRepository save()保存后未填充主键

  •  0
  • Jignesh M. Khatri  · 技术社区  · 7 年前

    @IdClass 在我的身体里。它在任何情况下都能正常工作,除了储蓄。保存实体后,JPA不会启动 SELECT

    抽象实体.java

    @MappedSuperclass
    @IdClass(PrimaryKey.class)
    public abstract class AbstractEntity implements Serializable {
    
        /** The Constant serialVersionUID. */
        private static final long serialVersionUID = -1191422925622832672L;
    
        /** The id. */
        private String id;
    
        ...
    
        /**
         * Gets the id.
         *
         * @return the id
         */
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        public String getId() {
            return id;
        }
    
        ...
    }
    

    PrimaryKey.java文件

    public class PrimaryKey implements Serializable {
    
        /** The id. */
        private String id;
    
        /**
         * Gets the id.
         *
         * @return the id
         */
        @Column(name = "id")
        @Convert(converter = CryptoConverter.class)
        public String getId() {
            return id;
        }
    
        ...
    }
    

    用户.java

    @Entity
    @Table(name = "user")
    public class User extends AbstractEntity {
        ...
    }
    

    用户存储库.java

    @Repository
    public interface UserRepository extends CrudRepository<User, PrimaryKey> {
    }
    

    BigInt autoIncrement 数据库中的Id作为主键。但我想以加密的形式向外界公开,所以我使用了 @Converter

    当我召唤 userRepository.save(userEntity) UserService ,它保存数据,但不返回从数据库生成的id。

    我怎样才能解决这个问题?

    我用这个功能主持了演示项目 here .

    0 回复  |  直到 7 年前
        1
  •  0
  •   Manolis Pap    6 年前

    因为我在你的代码中看不到任何地方,你需要指定 Id ,策略类型和数据库上的列。

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "applicant_id")