问题描述
create table logical_id_seq(
logical_id int auto_increment,
primary key(logical_id)
);
create table mytable(
physical_id int auto_increment,
logical_id int not null引用parent(logical_id),
data varchar(20),
主键(physical_id)
);
第二个表格使用第一个表格自动生成的值作为其值。我不知道如何在休眠模型中进行建模。
我读过,但我doesn看起来很难理解。
实际上很难说,我不知道你想在对象中表达什么级别:它是一对一的外键关联吗?多对一的关联?协会是双向的吗?使用ORM意味着思考对象不仅仅是表格,它通常有助于提供对象模型。
我假设这是一对一的外键关联。以下是 Java与Hibernate的持久性建议:
如果你真的有一个真正的多对一关联,应该很容易适应上述解决方案。
I have the below tables.
create table logical_id_seq (
logical_id int auto_increment,
primary key(logical_id)
);
create table mytable (
physical_id int auto_increment,
logical_id int not null references parent(logical_id),
data varchar(20),
primary key(physical_id)
);
The second table uses first table auto-generated value as its value. I am not sure how to model this in hibernate.
I read http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-onetoone, but I doesn't seem to understand.
It's actually hard to say, I don't know what you want to represent at the object level: is it a one-to-one foreign key association? a many-to-one association? is the association bi-directional? Using an ORM means thinking objects more than tables and it usually help to provide the object model.
I'll assume this is a one-to-one foreign key association. Here is what Java Persistence with Hibernate recommends:
If what you have is actually a real many-to-one association, it should be pretty easy to adapt the above solution.
这篇关于Hibernate,如何建模这种关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!