Hibernate:将foreign_key指向辅助表

时间:2017-02-28 09:36:20

标签: java database hibernate persistence hibernate-mapping

我有三个班级:

  • PlanItem
  • 任务
  • HumanTask的

它有以下层次结构:

public abstract class PlanItem {...}

public class Task extends PlanItem {...}

public class HumanTask extends Task {...}

我正在使用hibernate生成两个表:" PlanItem"具有所有属性 和"任务"具有所有属性和孩子的属性。

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class PlanItem {...}

@Entity
@SecondaryTable( name = "Task" )
public class Task extends PlanItem {
    @ElementCollection
    private Set<String> properties;

}

@Entity
@SecondaryTable( name = "Task" )
public class HumanTask extends Task {...}

Hibernate将为属性创建一个额外的表,但foreign_key将指向 到了&#34; PlanItem&#34;表。我怎样才能让foreign_key指向&#34;任务&#34;表

1 个答案:

答案 0 :(得分:1)

您可以尝试使用@ForeignKey

进行映射
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_action, menu);

        return true;
    }


@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        Toast.makeText(getApplicationContext(), "ItemSelected = " + item, Toast.LENGTH_SHORT).show();
             case R.id.action_sort:
             showPopup(item.getActionView());
     }

    return super.onOptionsItemSelected(item);
}


public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    Toast.makeText(getApplicationContext(), "showPopup = " + popup, Toast.LENGTH_SHORT).show();
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.popup_menu, popup.getMenu());
    popup.show();
    }

你可能需要一个最近的hibernate版本,因为我认为能够指定外键一直只在5.0 by HHH-9709中部分修复,而且只在5.2 by HHH-11180完全修复。