我为每个新实体类的所有实体创建一个基类来扩展它。 在基类中,我使用orm的注释,代码如下:
public class BaseEntity<I> implements IBaseEntity<I> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false)
private I id;
public I getId() {
return id;
}
public void setId(I id) {
this.id = id;
}}
然后我创建一个类如下:
@SuppressWarnings("serial")
@Entity
@Table(name = FoodEntity.TABLE_NAME, schema = "public")
public class FoodEntity extends BaseEntity<Long> {
public static final String TABLE_NAME = "T_Food";
@Column(name = "NAME", unique = true, nullable = false, length = 500)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}}
但是,当我执行一个示例来测试我的类时,我得到以下异常:
Initial SessionFactory creation failed.org.hibernate.AnnotationException: No identifier specified for entity: FoodEntity
Exception in thread "main" java.lang.ExceptionInInitializerError
at ir.msr.projects.crawler.HibernateUtil.buildSessionFactory(HibernateUtil.java:31)
at ir.msr.projects.crawler.HibernateUtil.<clinit>(HibernateUtil.java:14)
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: FoodEntity
at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:266)
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:731)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:249)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
答案 0 :(得分:3)
我找到了答案,我必须使用@MappedSuperclass作为BaseEntity Class(父类)。
答案 1 :(得分:0)
尝试创建BaseEntity抽象和anotated Id放入主类,或使用注释@MappedSuperclass
public class HighScoreScreenSlide extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
private int countDownInd;
Bundle bundle;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.high_score_view_pager);
countDownInd = getIntent().getIntExtra("gameType", 0);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(countDownInd);
InkPageIndicator inkPageIndicator = (InkPageIndicator) findViewById(R.id.indicator);
inkPageIndicator.setViewPager(mPager);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter( FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) { //use position
HighScoreFragment fragment = new HighScoreFragment();
bundle=new Bundle();
bundle.putInt("gameType",position);
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
或
@MappedSuperclass
public class BaseEntity<I> implements IBaseEntity<I> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false)
private I id;
public I getId() {
return id;
}
public void setId(I id) {
this.id = id;
}
}
}