3
回答
@JoinColumn如何控制不生成外键
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   
@Entity
@Table(name="users")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	@Column
	private String name;
	
	@OneToOne(optional=true)
	@JoinColumn(name="card_id", nullable=true, insertable=false, updatable=false, referencedColumnName="id")
	private IdCard idCard;

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public IdCard getIdCard() {
		return idCard;
	}
	public void setIdCard(IdCard idCard) {
		this.idCard = idCard;
	}
}



@Entity
@Table(name="id_cards")
public class IdCard {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	@Column(name="card_num")
	private String cardNum;
	
	@Column
	private String name;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCardNum() {
		return cardNum;
	}
	public void setCardNum(String cardNum) {
		this.cardNum = cardNum;
	}
}
启动应用,发现生成了如下SQL

Hibernate: create table id_cards (id integer not null auto_increment, card_num varchar(255), name varchar(255), primary key (id))
Hibernate: create table users (id integer not null auto_increment, name varchar(255), card_id integer, primary key (id))
Hibernate: alter table users add constraint FKe5nfq7ke10bu6e4ugj5mlj7pf foreign key (card_id) references id_cards (id)

有个外键,请问如何控制不让生成外键





举报
平西王
发帖于2年前 3回/460阅
顶部