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

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

问题描述

我一直在试图解决这一整天,但没有运气!我也试过在网上阅读大部分的教程,但你都知道他们都充满了无用的例子,不能反映你在现实世界中需要什么。



所以这里是我的情况:



数据库:



表:车辆 (vehicleId,brand,model,devYear,regNumber)< - vehicleId是PrimaryKey



表:其他(vehicleId,allowSmoke,allowFood ,allowDrinks,airConditioner)< - vehicleId是PK和FK。



关键是如果我有一个类 Vehicle 映射到数据库的类 TravelExtras 我希望 Vehicle 类具有 TravelExtras travelExtras属性并获取和设置方法。



不幸的是,不管我试图在databse中持久化对象,我得到了各种错误。



例如:

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


TravelExtra travelExtra = new TravelExtra();


entitymanager.persist(travelExtra);


车辆=新车辆(2L,10152487958556242,Mazda,626,334343,2005,4);
vehicle.setTravelExtra(travelExtra);

entitymanager.persist(vehicle);



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

emfactory.close();

任何人都知道这种一对一的注释使用什么样的注释?

$ Java持久性wikibook有一个名为

rel =nofollow>通过OneToOne和ManyToOne关系的主键,这似乎表明你想要的是可能的。



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

  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)
私家车;

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

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


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.

So here is my situation:

The database:

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

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

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.

Here is an illustration:

        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 ?

解决方案

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;
    }
}

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天全站免登陆