使用Spring boot 1.5.9和Hibernate在保存时获取Autogenerated Id

时间:2018-03-07 11:53:55

标签: hibernate spring-mvc spring-boot spring-data-jpa

我使用JpaRepository实现了持久性。我想在保存任何数据时生成Id而不使用任何额外的数据库操作。我也跟着this-approach但每次都返回0(零)而不是实际的id。以下是我的实施:

我的模特课:

@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(name = "registrations", schema = "sqs")
public class RegistrationsEntity implements Serializable {
    private long rId;
    private String rName;
    private String rPassword;
    private String rEmail;
    private Date rCreatedAt;
    private Date rUpdatedAt;

    @Id
    @Column(name = "r_id")
    public long getrId() {
        return rId;
    }

    public void setrId(long rId) {
        this.rId = rId;
    }

    @Basic
    @Column(name = "r_name")
    public String getrName() {
        return rName;
    }

    public void setrName(String rName) {
        this.rName = rName;
    }

    @Basic
    @Column(name = "r_password")
    public String getrPassword() {
        return rPassword;
    }

    public void setrPassword(String rPassword) {
        this.rPassword = rPassword;
    }

    @Basic
    @Column(name = "r_email")
    public String getrEmail() {
        return rEmail;
    }

    public void setrEmail(String rEmail) {
        this.rEmail = rEmail;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    @Basic
    @Column(name = "r_created_at", nullable = false, updatable = false)
    public Date getrCreatedAt() {
        return rCreatedAt;
    }

    public void setrCreatedAt(Date rCreatedAt) {
        this.rCreatedAt = rCreatedAt;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    @Basic
    @Column(name = "r_updated_at", nullable = false)
    public Date getrUpdatedAt() {
        return rUpdatedAt;
    }

    public void setrUpdatedAt(Date rUpdatedAt) {
        this.rUpdatedAt = rUpdatedAt;
    }
}

我的DAO课程:

@Repository
@Transactional
public class RegistrationDAOImpl implements IRegistrationDAO {

    @Autowired
    RegistrationRepository registrationRepository;

    @Override
    public void insertRegistration(RegistrationsEntity registrationsEntity) {

        registrationRepository.save(registrationsEntity);

        try {
            System.out.println("Reg Id " + registrationsEntity.getrId());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我的服务类:

@Service
public class RegistrationServiceImpl implements IRegistrationService {

    @Autowired
    private IRegistrationDAO iRegistrationDAO;

    @Override
    public boolean addRegistration(RegistrationsEntity registrationsEntity) {

        iRegistrationDAO.insertRegistration(registrationsEntity);
        return true;
    }
}

我的存储库界面:

public interface RegistrationRepository extends JpaRepository<RegistrationsEntity, Long> {
}

我的控制器:

@Controller
public class DummyController {

    @GetMapping(value = "regtest")
    @ResponseBody
    public void regtest() {

        RegistrationsEntity entity = new RegistrationsEntity();
        entity.setrName("ssssss");
        entity.setrPassword("12345");
        entity.setrEmail("ssss@rrrr.net");

        iRegistrationService.addRegistration(entity);
    }
}

请帮助......

1 个答案:

答案 0 :(得分:1)

我应该像这样声明:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

还应在数据库模式中设置自动增量属性(例如在MySQL中)。

你得到0的原因很可能是因为数据库没有自动生成id将其设置为默认值0.很可能你甚至没有在数据库模式中设置PK。