1

我正在尝试在我的应用程序中实现用户类型的继承结构。

@Entity
@Table(name="User_") // Need this since "User" is a reserved name in SQL Server
@Inheritance(strategy=InheritanceType.JOINED)
public class User extends Model {

    @Column(nullable=false,unique=true)
    public String username;

    ...
}

@Entity
public class Employee extends User {
    ...
}

@Entity
public class Client extends User {
    ...
}

当我这样做时,创建的EmployeeandClient表没有表的任何外键,User_表也没有or表User_的任何外键。我怎样才能做到这一点?EmployeeClient

理想情况下,我想要一个Employee.username作为 外键的列User_,因此其他表可以引用Employee.username.

我正在使用播放!框架 1.2.5 和 SQL Server 2005。

4

2 回答 2

0

Answering my own question since it's been a few days.

The first question I asked was:

When I do this, the Employee and Client tables that get created do not have any foreign keys to the User_ table, nor does the User_ table have any foreign keys to the Employee or Client tables. How can I make this happen?

This was answered in the comments, thanks axtavt! It turns out that id is the Primary Key and the Foreign Key.


Next up, I wrote this in my question:

Ideally, I'd like an Employee.username column that is a foreign key for User_, so I other tables can have references to Employee.username.

And clarified / worded as a question in comments:

Any way to actually have the username as a string show up in Employee? I want to be able to do this on other tables:

@ManyToOne
@JoinColumn(name="employee", referencedColumnName="username")
public Employee employee;

I was never able to find an adequate answer to this, so I'm making do with the default (using the numeric id field):

@ManyToOne
public Employee employee;

I'll be happy to accept another answer if this can be solved.

于 2012-09-13T19:07:44.447 回答
0

我想你正在寻找这个:http ://docs.oracle.com/javaee/5/api/javax/persistence/PrimaryKeyJoinColumn.html

另外,请注意您应该实现一个鉴别器值。

您可以在此处阅读该主题: http ://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#d0e1168

于 2012-09-06T07:07:25.290 回答