在hibernate中为外键设置属性int是否足够?

时间:2016-12-26 17:36:56

标签: java mysql hibernate foreign-keys

我想在一个hibernate java类中的Mysql表之间建立一个外键一对一的关系。

DB:

table1:

Id   int         PK
name varchar(10) NN

table2:

Id      int         PK
name    varchar(10) NN
tabe1Id int         FK

代码:

JavaClass1

@Entity
@Table(name="table1")
public class table1{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Id")
    private int Id;

    @Column(name="name")
    private String name;
}

JavaClass2

@Entity
@Table(name="table2")
public class table2{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Id")
    private int Id;

    @Column(name="name")
    private String name;

    @Id
    @Column(name="table1Id")
    private int JavaClass1Id;
}

这样写得足够吗?将JavaClass1Id存储在JavaClass2中作为整数而不在hibernate中创建另一个类的关系?

1 个答案:

答案 0 :(得分:0)

正如人们已经指出你正在寻找@OneToOne和JPA指南。这是一个基本的方法:

班级表1:

@Entity
@Table(name="table1")
public class table1{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Id")
    private int Id;

    @Column(name="name")
    private String name;

    // omitting constructors, getters, setters, toString, etc.
}

班级表2:

@Entity
@Table(name="table2")
public class table2{

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="Id")
    private int Id;

    @Column(name="name")
    private String name;

    @OneToOne // using default values for db schema
    private table1 t1; // notice how you have an actual reference to your object

    // omitting constructors, getters, setters, toString, etc.
}

演示:

public void demo() {
    table1 t1 = new table1("my table1 object");
    t1 = t1Repo.save(t1);

    table2 t2 = new table2("my table2 object", t1);
    t2 = t2Repo.save(t2);

    for (table2 t : t2Repo.findAll()) {
        log.info("retrieved t2 object " + t);
    }
}

打印:

retrieved t2 object table2{Id=1, name='my table2 object', t1=table1{Id=1, name='my table1 object'}}

这就是相应的MySQL的样子:

MariaDB [so41334487]> show tables;
+----------------------+
| Tables_in_so41334487 |
+----------------------+
| table1               |
| table2               |
+----------------------+
2 rows in set (0.00 sec)

MariaDB [so41334487]> describe table1;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.06 sec)

MariaDB [so41334487]> describe table2;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
| t1_id | int(11)      | YES  | MUL | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

MariaDB [so41334487]> select * from table1;
+----+------------------+
| id | name             |
+----+------------------+
|  1 | my table1 object |
+----+------------------+
1 row in set (0.00 sec)

MariaDB [so41334487]> select * from table2;
+----+------------------+-------+
| id | name             | t1_id |
+----+------------------+-------+
|  1 | my table2 object |     1 |
+----+------------------+-------+
1 row in set (0.00 sec)

以下是从CREATE中提取的相应SHOW CREATE TABLE table2语句:

CREATE TABLE `table2` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) DEFAULT NULL,
    `t1_id` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `FK30i46miycdu2aqony71wj8kva` (`t1_id`),
    CONSTRAINT `FK30i46miycdu2aqony71wj8kva` FOREIGN KEY (`t1_id`) REFERENCES `table1` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 

那里有很多高质量的内容,包括@OneToOne上的书籍和视频讲座以及相应的陷阱,但是如果你正在寻找一个快速的单挑,你可以开始{{3 }}。还要记住,如果在(一系列)对象之间创建循环引用,则链接FetchType.EAGER可能会导致您将整个数据库加载到内存中。