[英]jpa @manytoone creates composite primary key instead of @id
I am running spring jpa project and I have following class containing @ManyToOne relations: 我正在运行spring jpa项目,并且我有以下包含@ManyToOne关系的类:
package ba.fit.vms.pojo;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="korisnik_vozilo")
public class KorisnikVozilo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id;
@ManyToOne( cascade = {CascadeType.REFRESH}, fetch=FetchType.EAGER )
@JoinColumn(nullable=false, updatable=false)
private Vozilo vozilo;
@ManyToOne( cascade = {CascadeType.REFRESH}, fetch=FetchType.EAGER )
@JoinColumn(nullable=false, updatable=false)
private Korisnik korisnik;
@Column(name = "dodijeljeno")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@NotNull
private Date dodijeljeno;
@Column(name = "vraceno")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date vraceno;
...
}
For some reason, starting the server, this code creates composite primary key (korisnik_id,vozilo_vin), instead of @Id defined primary key. 由于某种原因,启动服务器后,此代码将创建复合主键(korisnik_id,vozilo_vin),而不是@Id定义的主键。 Here is the screenshot of the table: 这是表格的屏幕截图: Can someone explain to me what did I do wrong and how to write this code so I do not get this composite key in the database, but the one defined in the class. 有人可以向我解释我做错了什么以及如何编写此代码,因此我没有在数据库中获得此复合键,而是在类中定义了该复合键。
It even sets autoincrement on korisnik_id! 它甚至在korisnik_id上设置自动增量!
Try getting rid of updatable=false
. 尝试摆脱updatable=false
。 being unupdatable, the two columns might be be viewed as an immutable identity of the entity. 由于不可更新,这两列可能被视为实体的不变标识。
您可以检查如何使用@JoinColumn
解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.