JPA:外键也是主键映射 [英] JPA: Foreign key that is also a primary key mapping

查看:22
本文介绍了JPA:外键也是主键映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一整天都在努力解决这个问题,但没有成功!此外,我尝试阅读网上的大部分教程,但众所周知,它们都充满了无用的示例,无法反映您在现实世界中的需求.

I have been trying to solve this for whole day but no luck! Also i tried to read most of the tutorials on the net but as you all know they all are full of useless examples that do not reflect what you need in the real world.

这是我的情况:

数据库:

table: vehicles(vehicleId, brand, model, devYear, regNumber) <-- VehicleId 是主键

table: vehicles(vehicleId, brand, model, devYear, regNumber) <-- vehicleId is the PrimaryKey

table:extras(vehicleId、allowSmoke、allowFood、allowDrinks、airConditioner)<-- VehicleId 是 PK 和 FK.

table: extras(vehicleId, allowSmoke, allowFood, allowDrinks, airConditioner) <-- vehicleId is a PK and a FK.

关键是,如果我有一个类 Vehicle 和一个类 TravelExtras 映射到数据库,我希望 Vehicle 类有一个属性 TravelExtras travelExtras 以及 get 和 set 方法.

The point is that if i have a class Vehicle and a class TravelExtras which are mapped to the database i want the Vehicle class to have an attribute TravelExtras travelExtras and get and set methods.

不幸的是,当我尝试将对象持久保存在数据库中时,无论我尝试了什么,我都会遇到各种错误.

Unfortunatelly no matter what i tried when i try to persist the object in the databse i get various errors.

这是一个说明:

        EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "NaStopPU" );
        EntityManager entitymanager = emfactory.createEntityManager( );
        entitymanager.getTransaction( ).begin( );


        TravelExtra travelExtra = new TravelExtra();


        entitymanager.persist(travelExtra);


        Vehicle vehicle = new Vehicle(2L, "10152487958556242", "Mazda", "626", "334343", 2005, 4);  
        vehicle.setTravelExtra(travelExtra);

        entitymanager.persist(vehicle);



        entitymanager.getTransaction().commit();
        entitymanager.close( );

        emfactory.close( );

有谁知道这种一对一的情况应该使用什么样的注解?

Any one knows what kind of annotations to use for this One to one case ?

推荐答案

Java Persistence wikibook 有一个部分叫做 通过 OneToOne 和 ManyToOne 关系的主键 这似乎表明你想要的是可能的.

The Java Persistence wikibook has a section called Primary Keys through OneToOne and ManyToOne Relationships which seems to indicate that what you want is possible.

如果我没看错的话,对于你的情况,它看起来像:

If I'm reading it right, for your case, it would look something like:

class Vehicle {
    @Id
    @Column(name = "EXTRAS_ID")
    private long extrasId;

    @OneToOne(mappedBy="vehicle", cascade=CascadeType.ALL)
    private TravelExtra extras;
}

class TravelExtras {
    @Id
    @Column(name = "VEHICLE_ID")
    private long vehicleId;

    @OneToOne
    @PrimaryKeyJoinColumn(name="VEHICLE_ID", referencedColumnName="EXTRAS_ID")
    private Vehicle vehicle;

    public TravelExtras(Vehicle vehicle) {
        this.vehicleId = vehicle.getId();
        this.vehicle = vehicle;
    }
}

请注意,您的一个实体需要确保它与另一个实体具有相同的 id,这在示例中由 TravelExtras 构造函数完成,需要它绑定到的 Vehicle.

Note that one of your entities will need to make sure it has the same id as the other, which is accomplished in the example by the TravelExtras constructor requiring the Vehicle it is bound to.

这篇关于JPA:外键也是主键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆