我正在使用hibernate映射生成模式。但由于某种原因,一对一映射无法正确生成。这是我的课程:
@Entity
@Table(name = "resturant")
public class Restaurant {
private Integer restid;
private String restaurantName;
private Foursquare foursquare;
@Id
@Column(name = "restid")
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return restid;
}
public void setId(Integer id) {
this.restid = id;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "restaurant", cascade = CascadeType.ALL)
public Foursquare getFoursquare() {
return foursquare;
}
public void setFoursquare(Foursquare foursquare) {
this.foursquare = foursquare;
}
@Column(name = "restname")
public String getRestaurantName() {
return restaurantName;
}
public void setRestaurantName(String restaurantName) {
this.restaurantName = restaurantName;
}
}
和
@Entity
@Table(name = "foursquare")
public class Foursquare {
private Integer foursquareid;
private Restaurant restaurant;
@Id
@Column(name = "fsid")
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getFoursquareid() {
return foursquareid;
}
public void setFoursquareid(Integer foursquareid) {
this.foursquareid = foursquareid;
}
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public Restaurant getRestaurant() {
return restaurant;
}
public void setRestaurant(Restaurant restaurant) {
this.restaurant = restaurant;
}
}
我的hbm文件如下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- You need to complete the configuration here. This is just a sample,
you should use a connection pool -->
<property name="connection.url">jdbc:mysql://localhost:3306/menus3</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="Restaurant" />
<mapping class="Foursquare" />
</session-factory>
</hibernate-configuration>
这是我的HibernateUtil类:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public final class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
private HibernateUtil() {
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
我正在运行一个简单的类来通过加载配置来生成模式:
public class Test {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
session.close();
}
}
这应该在Foursquare表中创建restid的外键,但事实并非如此。 sql看起来像:
Hibernate: drop table if exists foursquare
Hibernate: drop table if exists resturant
Hibernate: create table foursquare (fsid integer not null auto_increment, idinfoursquare varchar(255), primary key (fsid))
Hibernate: create table resturant (restid integer not null auto_increment, restname varchar(255), staddress varchar(255), primary key (restid))
有人能指出为什么一对一的映射没有反映在我的数据库中吗?为什么没有生成外键列?
答案 0 :(得分:0)
您使用了@PrimaryKeyJoinColumn
。 @PrimaryKeyJoinColumn
注释确实表示实体的主键用作关联实体的外键值。所以外键没有生成。要生成外键,您应该删除@PrimaryKeyJoinColumn
注释。
@OneToOne(fetch = FetchType.LAZY)
//@PrimaryKeyJoinColumn Remove this annotation.
public Restaurant getRestaurant() {
return restaurant;
}