Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

8.8. Spring Data FAQ

8.8.1. No identifier specified for entity

8.8.2. Oracle Date 類型顯示日期和時間

			
package cn.netkiller.api.domain.oracle;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "test")
public class Test {

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "test_id_Sequence")
	@SequenceGenerator(name = "test_id_Sequence", sequenceName = "test")
	private Long id;

	@NotNull
	@DateTimeFormat(iso = ISO.DATE_TIME)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date createdate;

	public Member() {

	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

}
			
			

8.8.3. java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

問題描述,Restful 請求返回錯誤,檢查資料庫 BigInt 修改為 無符號整形,錯誤依舊存在

			
ALTER TABLE `cms`.`comment` 
CHANGE COLUMN `user_id` `user_id` INT(10) UNSIGNED NULL DEFAULT NULL ;
			
			

去掉 UNSIGNED 後,錯誤消失

			
ALTER TABLE `cms`.`comment` 
CHANGE COLUMN `user_id` `user_id` INT NULL DEFAULT NULL ;
			
			

Java 認為 INT(10) UNSIGNED 是 Long 型。

8.8.4. Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessApiUsageException","message":"Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query"