2

I have a table structure like this

Publisher:
- pid (primary key)
- ... other columns

Advertiser
- aid (primary key)
- ... other columns

User
- user_id (primary key)
- source_id (foreign key to either advertiser.aid or publisher.pid)
- source_type (string value either 'publisher' or 'advertiser')

I want to create a one to one mapping in my publisher (and eventually in my advertiser) object of Publisher to User. 'source_id' and 'source_type' form a composite key that can be used to link one to one to either publisher or advertiser.

I currently am trying this but it doesn't work, it just grabs the first 'User' row that matches which could be an advertiser.

public class Publisher implements Serializable {
    @Id
    @Column(name = "PID", unique = true, nullable = false, precision = 22, scale = 0)
    private Integer pid;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PID", referencedColumnName = "SOURCE_ID")
    @Where(clause = "source_id = pid AND source_type='publisher'")
    @NotFound(action = NotFoundAction.IGNORE)
    private Users users;    
}

public class Users implements Serializable {

    @Column(name = "USER_ID", nullable = false) 
    private Integer userId;

    @Column(name = "USER_TYPE",  length = 100)
    private String userType;

    @Id
    @Column(name = "SOURCE_ID", precision = 22, scale = 0)
    private Integer sourceId;
}

The where clause does not appear to be doing anything.

Thanks in advance.

EDIT: The DiscriminatorValue did not help. The closest thing I've found so far is something along these lines: http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

It turns out that my publisher entity actually had a 'user_id' which allowed me to just do a simple onetoone mapping. So I'm up and running again.

AaronP
  • 196
  • 2
  • 11
  • possible duplicate of [When to use DiscriminatorValue annotation in hibernate](http://stackoverflow.com/questions/16772370/when-to-use-discriminatorvalue-annotation-in-hibernate) – Shahzeb Jul 29 '15 at 05:04

0 Answers0