@JoinColumn如何控制不生成外键

平西王 发布于 2016/10/26 14:19
阅读 959
收藏 0
@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)

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





加载中
0
mytaia
mytaia
hibernate.hbm2ddl.auto=none
554330833a
554330833a
设置了这个就查询可以级联,而且又不会创建外键了吗?但是数据库表HB也不会新建吧
0
平西王
平西王

引用来自“mytaia”的评论

hibernate.hbm2ddl.auto=none
我就是要hibernate自动生成表
0
Mr_K
Mr_K
把注解去掉就行了!去掉注解关联~
554330833a
554330833a
这样的话 查不出数据,不能级联查询,属性都是null啊
返回顶部
顶部