Ich habe eine einfache Entität. Ich verwende spring-data-jpa Version 1.2.0.RELEASE und eclipselink 2.4.1.Warum ID ist nicht in der Entität von Frühling-Daten-jpa gespeichert speichern
@Entity
@Table(name="platform")
public class Platform {
@Id
@Column(name="id", nullable=false, updatable=false, insertable=true)
private Long id;
// etc.
}
Ich möchte es speichern. Mein Repository sieht aus wie
public interface PlatformRepository extends JpaRepository<Platform, Long> {
Platform findByName(String name);
}
My-Controller mit dieser Methode sehr einfach ist
@RequestMapping(method=RequestMethod.POST, produces="application/json")
public Platform post(Platform platform) {
Platform result = platformDao.saveAndFlush(platform);
return result;
}
Und die Antwort von dieser Methode ist
{"platform":{"id":null,"name":"Test1"}}
Select * von Plattform zeigt, dass Test1 eine ID von 6. Die Tabelle ist definiert als:
create table platform(
id int not null auto_increment primary key,
name varchar(128) not null);
Ich erwarte, dass die ID nach dem Speichern gesetzt wird, aber es ist nicht. Sie erwarten nicht, dass ich sofort nach dem Schreiben der Entity nachdenke, oder?
Nur eine Vermutung: versuchen Sie, "id" von "Long" zu "int" und "insertable = true" zu "false" zu ändern. Auch [* this *] (http://stackoverflow.com/questions/12122489/jparepository-transaction-and-repository-saveandflush) könnte relevant sein. – alfasin