解释用例
每次用户获得我的应用程序页面时,我都会在USAGE\u LOG表中插入一行(带有用户id和页面id)。
我的表格结构
table USAGE_LOG (for information only, this one works well)
ID PrimaryKey
USER_ID Foreign key to column ID of table USER
USAGE_LOG_PAGE_ID Foreign key to column ID of table USER_LOG_PAGE
table USAGE_LOG_PAGE
ID PrimaryKey
URL VARCHAR
USER_ACTION_ID Foreign key to column ID of table USER_ACTION
table USER_ACTION
ID PrimaryKey
ACTION_CATEGORY_ID Foreign key to column ID of table ACTION_CATEGORY
ACTION_TYPE_ID Foreign key to column ID of table ACTION_CATEGORY
table ACTION_CATEGORY
ID PrimaryKey
NAME VARCHAR
table ACTION_TYPE
ID PrimaryKey
NAME VARCHAR
因此,USER\u ACTION表是一个连接表,其特殊性在于它同时将USAGE\u LOG\u页面链接到ACTION\u类别和ACTION\u类型。
不幸的是,我无法更改数据库结构(这是遗留代码)。
@ManyToOne
@JoinTable(name="action",
joinColumns=@JoinColumn(name="ID", referencedColumnName="USER_ACTION_ID"),
inverseJoinColumns=@JoinColumn(name="ACTION_CATEGORY_ID", referencedColumnName="ID"))
@Getter @Setter
private ActionCategory actionCategory;
@ManyToOne
@JoinTable(name="action",
joinColumns=@JoinColumn(name="ID", referencedColumnName="USER_ACTION_ID"),
inverseJoinColumns=@JoinColumn(name="ACTION_TYPE_ID", referencedColumnName="ID"))
@Getter @Setter
private ActionType actionType;
(我用龙目来表示@Getter和@Setter)
此映射可以编译,但当我尝试获取数据时,出现以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列“usagelogpa0”。“字段列表”中的“actionCategory”
实际上,Hibernate查询是:
select usagelogpa0_.ID as ID1_80_0_,
usagelogpa0_.actionCategory as actionCa2_80_0_,
usagelogpa0_.actionType as actionTy3_80_0_,
usagelogpa0_.URL as URL5_80_0_
from usage_log_page usagelogpa0_
where usagelogpa0_.ID=?
(关键部分是select中的“actionCategory”和“actionType”)
这不是我所期望的,Hibernate应该做一个连接。