继承映射策略分为三种类型 -
每个类层次结构的表
每个具体类的表
每个子类的表
在本文中,我们将讨论每个类的表层次结构。
在此,仅创建一个表用于继承映射。这种方法的缺点是表中存储了大量空值。
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)、@DiscriminatorColumn 和 @DiscriminatorValue 是此策略中使用的注解。
@DiscriminatorColumn 用于创建一个附加列,用于标识层次结构类。
考虑以下示例来理解这一点 -
实施步骤 -
创建实体类并为其使用适当的注释。
编写 hibernate 配置文件并添加映射类。
编写代码来创建数据并将其存储在表中。
package com.tutorialspoint; @Entity @Table(name = "car") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="category",discriminatorType=DiscriminatorType.STRING) @DiscriminatorValue(value="car") public class Car { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column(name = "name") private String name; @Column(name = "color") private String color; //Getters //Setters }
package com.tutorialspoint; import javax.persistence.*; @Entity @DiscriminatorValue("sportscar") public class Sports_Car extends Car{ @Column(name="mileage") private int mileage; @Column(name="cost") private int cost; //Getters //Setters }
package com.tutorialspoint; import javax.persistence.*; @Entity @DiscriminatorValue("taxicar") public class Taxi_Car extends Car{ @Column(name="farePerKm") private int farePerKm; @Column(name="available") private boolean available; //Getters //Setters }
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- JDBC Database connection settings --> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/demo?useSSL=false</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool settings ... using built-in test pool --> <property name="connection.pool_size">4</property> <!-- Echo the SQL to stdout --> <property name="show_sql">true</property> <!-- Select our SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create-drop</property> <!-- name of annotated entity class --> <mapping class="com.tutorialspoint.Car"/> <mapping class="com.tutorialspoint.Sports_Car"/> <mapping class="com.tutorialspoint.Taxi_Car"/> </session-factory> </hibernate-configuration>
package com.tutorialspoint; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class StoreTest { public static void main(String args[]){ SessionFactory sessionFactory = new Configuration() .configure("com/tutorialspoint/hibernate.cfg.xml") .buildSessionFactory(); Session session=sessionFactory.openSession(); Transaction t=session.beginTransaction(); Car c1=new Car(); c1.setName("Mercedes"); c1.setColor("Black"); Sport_Car c2=new Sport_Car(); c2.setName("Porsche"); c2.setColor("Red"); c2.setMileage(20); c2.setCost(5000000); Taxi_Car c3=new Taxi_Car(); c3.setName("Innova"); c3.setColor("White"); c3.setFarePerKm(7); c3.setAvailable(true); session.persist(c1); session.persist(c2); session.persist(c3); t.commit(); session.close(); } }
以上是在Hibernate中有哪些不同的继承映射策略?的详细内容。更多信息请关注PHP中文网其他相关文章!