Kotlin JpaRepository保存新的empy对象以进行更新

时间:2018-08-03 23:55:58

标签: spring-boot kotlin spring-data-jpa

因此,我正在使用Spring Boot和Kotlin。发布对象时,如果数据库中有一个对象,则使用uuid进行查找。如果这样做,我将修改该实例,否则使用一个新实例。

当我查看数据库时,我看到了更新的版本,然后看到了一个新的空行。

我可以看到更新的时间戳,如果我使用调试器来操纵它保存的值。但是总会有一个新行,带有新ID,并且所有字段均为空

服务

@Entity
data class Claim (

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    var uuid: String = ""
    )
    {

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    var claimNo: Long = 0

    var status: ClaimStatus = ClaimStatus.CREATED

    var billingDate: LocalDate = LocalDate.now()

    var lastUpdated: LocalDateTime = LocalDateTime.now()

    var facility: String = ""

    var payProgram: String = ""

    var createdAt:LocalDateTime = LocalDateTime.now()

    var createdBy: String = ""

    var dxCode: String = ""

    var notes: String = ""

    @ManyToOne(cascade = [CascadeType.ALL])
    var patient: Patient = Patient()

    @ManyToOne(cascade = [CascadeType.ALL])
    var doc: Doctor = Doctor()

    @OneToMany(cascade = [CascadeType.ALL], mappedBy = "claim")
    var billedCodes: MutableList<BilledCodes> = mutableListOf<BilledCodes>()

    @PrePersist
    @PreUpdate
    fun updateClaim()
    {
        lastUpdated = LocalDateTime.now()
    }
}

索赔

@Repository
interface ClaimRepository : CrudRepository<Claim, String>

存储库

5bdf8247-5c66-4a9e-9101-450588d5e6b8    2018-01-01  2   2018-08-03 20:41:03 TestPoint   1234    1234    2018-08-03 20:56:41 ""  HCP 0   1   47
a9200e84-643b-409a-9c44-2b69aa8a9823    2018-08-03  0   2018-08-03 20:56:41 ""  ""  ""  2018-08-03 20:56:41 ""  ""  0   23  46
16249817-6f4b-46df-a959-ec0595bb06d7    2018-01-01  0   2018-08-03 20:57:03 TestPoint   1234    1234    2018-08-03 20:57:03 ""  HCP 0   1   48
6799275b-e067-439e-aec1-2f9166e9b2ba    2018-08-03  0   2018-08-03 20:57:03 ""  ""  ""  2018-08-03 20:57:03 ""  ""  0   24  49
76c2fd49-5a92-4e3a-a8db-d882beb635e2    2018-01-01  0   2018-08-03 21:07:14 TestPoint   1234    1234    2018-08-03 21:07:14 ""  HCP 0   1   50
fec419d9-ae9d-4d53-a287-add14b1cd373    2018-08-03  0   2018-08-03 21:07:14 ""  ""  ""  2018-08-03 21:07:14 ""  ""  0   25  51

@Component
class ApplicationStartup: ApplicationListener<ApplicationReadyEvent>
{
    @Autowired
    lateinit var claimService: ClaimService
    override fun onApplicationEvent(event: ApplicationReadyEvent) {
        //var claimService: ClaimService = ClaimService()
        var testStr = "{\"claim\":{\"uuid\": \"5bdf8247-5c66-4a9e-9101-450588d5e610\",\"claimNo\": 123,\"status\": \"CREATED\",\"claimDate\": \"2018-01-01\",\"facility\": \"1234\",\"dxCode\": \"1234\",\"notes\": \"test notes\",\"payProgram\": \"HCP\",\"patient\": {\"name\": \"first last\",\"DOB\": \"1992-05-06\",\"ohipNo\": \"123456789\",\"verCode\": \"AA\",\"sex\": \"M\"},\"billedCodes\": [{\"code\": \"A001A\",\"amountBilled\": 19.21,\"amountPaid\": 0.0}]}}"
        val mapper = jacksonObjectMapper()
        val claimTest = mapper.readValue<ClaimDto>(testStr)
        claimService.saveClaim(claimTest)
    }
}

测试功能

{{1}}

1 个答案:

答案 0 :(得分:0)

所以答案是我对帐单代码进行映射的方式。删除该关系不再重复保存。

关系需要@Transient标志,而计费代码的映射则需要映射人