简体   繁体   中英

JPA Unique constraint on nested entities

Having three entities, that are nested, say A{b} , B{c} , C{id} Can I use JPA's unique constraint annotation to access C's id on A?

My scenario: I have some other field "z" on A, and I want to impose that "z" and c's "id" are uniquely associated

Queries

You can join those tables without any configuration changes using the 'old' style join. It enables you to link entities by columns which are not linked directly:

select a 
From A a, C c
Where a.z = c.id

Mapping

If you want to change the configuration try this:

public class C{

    @ManyToOne
    @JoinColumn(name = "id", referencedColumnName = "z")
    private A a;

}

public class A{

    @OneToMany(mappedBy = "a"
    @JoinColumn(name = "z", referencedColumnName = "id")
    private List<C> cCollection;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM