2016-04-03 67 views
0

我使用觸發器在Oracle數據庫中自行增加表的id列。通過Spring MVC從Oracle數據庫獲取自動遞增的ID(主鍵)

這裏是我的春節,控制器代碼,

@RequestMapping("/ssuForm/create") 
@ResponseStatus(HttpStatus.OK) 
@ResponseBody 
public UmkeiBusinessInfo createSsuForm(@RequestBody UmkeiBusinessInfo umkeiSsu) { 

    UmkeiBusinessInfo createSsuForm = umkeiBusinessInfoService.create(umkeiSsu); 
    System.out.println(createSsuForm); 
    return createSsuForm; 
} 

如何在使用的System.out.println看到返回的東西,我看不到被列出的ID。一切儘管。

這裏的日誌,

|Nexus|Wed Apr 13 00:00:00 SGT 2016|Nexus Street|NexusCity|04|null|80000|0108808550|[email protected]|1|A|2|P1|B10A218|Sun Apr 03 10:40:28 SGT 2016|null||B10A218|1|0145283459|[email protected]|9|A08|00751A|null|null|null|null|null 

注意到它與Nexus,而不是ID開始。

下面是什麼我的表看起來像片斷, enter image description here

編輯:

這裏面的代碼/業務/服務/ IMPL

@Override 
public UmkeiBusinessInfo save(UmkeiBusinessInfo umkeiBusinessInfo) { 
    return update(umkeiBusinessInfo) ; 
} 

@Override 
public UmkeiBusinessInfo create(UmkeiBusinessInfo umkeiBusinessInfo) { 
    UmkeiBusinessInfoEntity umkeiBusinessInfoEntity = umkeiBusinessInfoJpaRepository.findOne(umkeiBusinessInfo.getUbiId()); 
    if(umkeiBusinessInfoEntity != null) { 
     throw new IllegalStateException("already.exists"); 
    } 
    umkeiBusinessInfoEntity = new UmkeiBusinessInfoEntity(); 
    umkeiBusinessInfoServiceMapper.mapUmkeiBusinessInfoToUmkeiBusinessInfoEntity(umkeiBusinessInfo, umkeiBusinessInfoEntity); 
    UmkeiBusinessInfoEntity umkeiBusinessInfoEntitySaved = umkeiBusinessInfoJpaRepository.save(umkeiBusinessInfoEntity); 
    return umkeiBusinessInfoServiceMapper.mapUmkeiBusinessInfoEntityToUmkeiBusinessInfo(umkeiBusinessInfoEntitySaved); 
} 

@Override 
public UmkeiBusinessInfo update(UmkeiBusinessInfo umkeiBusinessInfo) { 
    UmkeiBusinessInfoEntity umkeiBusinessInfoEntity = umkeiBusinessInfoJpaRepository.findOne(umkeiBusinessInfo.getUbiId()); 
    umkeiBusinessInfoServiceMapper.mapUmkeiBusinessInfoToUmkeiBusinessInfoEntity(umkeiBusinessInfo, umkeiBusinessInfoEntity); 
    UmkeiBusinessInfoEntity umkeiBusinessInfoEntitySaved = umkeiBusinessInfoJpaRepository.save(umkeiBusinessInfoEntity); 
    return umkeiBusinessInfoServiceMapper.mapUmkeiBusinessInfoEntityToUmkeiBusinessInfo(umkeiBusinessInfoEntitySaved); 
} 

這裏是實體部分,

//---------------------------------------------------------------------- 
// ENTITY PRIMARY KEY (BASED ON A SINGLE FIELD) 
//---------------------------------------------------------------------- 
@NotNull 
@Size(min = 1, max = 20) 
private String ubiId; 

//---------------------------------------------------------------------- 
// ENTITY DATA FIELDS 
//----------------------------------------------------------------------  
@Size(max = 100) 
private String ubiName; 


private Date ubiStartDate; 

@Size(max = 100) 
private String ubiAddress; 

@Size(max = 64) 
private String ubiCity; 

@Size(max = 10) 
private String ubiState; 

@Size(max = 10) 
private String ubiCountry; 

這裏是JPA實體部分,

//---------------------------------------------------------------------- 
// ENTITY PRIMARY KEY (BASED ON A SINGLE FIELD) 
//---------------------------------------------------------------------- 
@Id 
@Column(name="UBI_ID", nullable=false, length=20) 
private String  ubiId  ; 


//---------------------------------------------------------------------- 
// ENTITY DATA FIELDS 
//----------------------------------------------------------------------  
@Column(name="UBI_NAME", length=100) 
private String  ubiName  ; 

@Temporal(TemporalType.TIMESTAMP) 
@Column(name="UBI_START_DATE") 
private Date  ubiStartDate ; 

@Column(name="UBI_ADDRESS", length=100) 
private String  ubiAddress ; 
+1

爲什麼在世界上你會使用觸發器來增加id並且不使用本地音序器? – pczeus

+0

糟糕,我啓用了觸發器,並自動激活序列。啓用觸發器後,我什麼也沒做。 – Luqman

+1

如果您使用的是JPA – Bunti

回答

0

我剛剛得到它的工作。

首先,我需要在ORACLE中將數據類型更改爲'Number',然後將所有代碼從String更改爲BigDecimal。

然後添加註釋像Bunti提到,

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="UMKEI_BUSINESS_INFO_SEQ1") 
@SequenceGenerator(name="UMKEI_BUSINESS_INFO_SEQ1 ", sequenceName="UMKEI_BUSINESS_INFO_SEQ1", allocationSize=1) 

,我得到了我的增量值傳遞給客戶端。

以前它因dataType而無法工作。