我正在学习Hibernate框架(5.xx),在使用注释的每个具体类继承实现中,Table一直存在问题。
下面是代码结构:
-> Employee.java
@Entity
@Table(name="TPCEmp")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Employee implements Serializable {
@Id
@Column(name="empId",nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int Id;
@Column(name="empName")
private String name;
@Column(name="emp_joinDate")
@Temporal(javax.persistence.TemporalType.DATE)
private Date joinDate;
@Column(name="emp_work_hours")
private int work_hours;
//getters & setters
}
-> Techie.java
@Entity
@Table(name = "TPCTech")
@AttributeOverrides({
@AttributeOverride(name = "techempId", column = @Column(name = "empId")),
@AttributeOverride(name = "techempName", column = @Column(name = "empName")),
@AttributeOverride(name = "techemp_joinDate", column = @Column(name = "emp_joinDate")),
@AttributeOverride(name = "techemp_work_hours", column = @Column(name = "emp_work_hours"))
})
public class Techie extends Employee {
@Column(name = "language_used")
private String language_used;
//getters & setters
}
-> Support.java
@Entity
@Table(name = "TPCSupport")
@AttributeOverrides({
@AttributeOverride(name = "supportempId", column = @Column(name = "empId")),
@AttributeOverride(name = "supportempName", column = @Column(name = "empName")),
@AttributeOverride(name = "supportemp_joinDate", column = @Column(name = "emp_joinDate")),
@AttributeOverride(name = "supportemp_work_hours", column = @Column(name = "emp_work_hours"))
})
public class Support extends Employee {
@Column(name = "tool_used")
private String tool_used;
//getters & setters
}
-> hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">Shashi.742744</property>
<mapping class="tableperconcreteclassinhierarchy.Employee"/>
<mapping class="tableperconcreteclassinhierarchy.Techie"/>
<mapping class="tableperconcreteclassinhierarchy.Support"/>
</session-factory>
</hibernate-configuration>
在执行程序时,出现此异常:
Exception in thread "main" org.hibernate.AnnotationException: An entity annotated with @Inheritance cannot use @AttributeOverride or @AttributeOverrides: tableperconcreteclassinhierarchy.Techie
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:566)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
at tableperconcreteclassinhierarchy.TablePerConcreteClassinHierarchy.main(TablePerConcreteClassinHierarchy.java:30)
->主类代码:
public static void main(String[] args) {
// TODO code application logic here
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata md = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory sf = md.buildSessionFactory();
Session sess = sf.openSession();
Transaction t = sess.beginTransaction();
Employee emp = new Employee();
emp.setName("emp1");
emp.setJoinDate(Date.from(Instant.now()));
emp.setWork_hours(8);
Techie tech = new Techie();
tech.setName("tech1");
tech.setJoinDate(Date.from(Instant.now()));
tech.setWork_hours(8);
tech.setLanguage_used("Java");
Support sp = new Support();
sp.setName("support1");
sp.setJoinDate(Date.from(Instant.now()));
sp.setWork_hours(8);
sp.setTool_used("CSC");
sess.persist(emp);
sess.persist(tech);
sess.persist(sp);
t.commit();
sess.close();
sf.close();
ssr.close();
}
那么,我是否实施了这个错误?还是我缺少任何需要的额外注释?
任何见解都会很有帮助。
先谢谢了。
答案 0 :(得分:0)
您可以从每个子类类中删除@AttributeOverrides批注,并将Employee类上的GenerationType.AUTO更改为GenerationType.TABLE。
GenerationType.TABLE