我有如下情况: 我的应用程序中有两个模块:模块A和模块B。 我的应用程序具有下表: 客户,经销商,交易,产品和提供者。
@Entity
@Table(name ="client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CLIENT_ID", unique = true, nullable = false)
private Long id;
private String name;
private String email;
private String phone;
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "DEALER_ID")
private Dealer dealer;
//getters and setters
}
@Entity
@Table(name ="dealer")
public class Dealer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "DEALER_ID", unique = true, nullable = false)
private Long id;
private String name;
private String email;
private String phone;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY, mappedBy = "dealer")
private Set<Client> clients;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY, mappedBy = "dealer")
private Set<Deal> deals;
//getters ans setters
}
@Entity
@Table(name ="deal")
public class Deal {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "DEAL_ID", unique = true, nullable = false)
private Long id;
@ManyToMany(cascade= {CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.MERGE},fetch= FetchType.LAZY)
@JoinTable(name="deal_product", joinColumns={
@JoinColumn(name="DEAL_ID", nullable=false, updatable=false )},
inverseJoinColumns = {@JoinColumn(name = "PRODUCT_ID", nullable=false, updatable = false) })
private Set<Product> products;
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "DEALER_ID")
private Dealer dealer;
//getters ans setters
}
@Entity
@Table(name ="product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PRODUCT_ID", unique = true, nullable = false)
private Long id;
private String name;
private double price;
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "PROVIDER_ID")
private Provider provider;
//getters ans setters
}
@Entity
@Table(name ="provider")
public class Provider {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PROVIDER_ID", unique = true, nullable = false)
private Long id;
private String name;
private String email;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY, mappedBy = "provider")
private Set<Product> products;
//getters and setters
}
模块A只是一个计划系统,供客户查找其经销商的联系信息。 模块B是经销商管理系统。
为了让Hibernate仅上传每个模块所需的表,我在运行时创建了配置,如下所示:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration cfg = new Configuration();
cfg.setProperty("hibernate.connection.driver_class","????");
cfg.setProperty("hibernate.connection.url","????");
cfg.setProperty("hibernate.connection.username","????");
cfg.setProperty("hibernate.connection.password","????");
cfg.setProperty("hibernate.enable_lazy_load_no_trans","???");
cfg.setProperty("hibernate.connection.pool_size", "???");
cfg.setProperty("hibernate.dialect","?????");
cfg.setProperty("hibernate.current_session_context_class","??");
cfg.setProperty("hibernate.cache.provider_class","???");
cfg.setProperty("hibernate.show_sql", "??");
cfg.setProperty("hibernate.hbm2ddl.auto", "??");
cfg.addAnnotatedClass(model.Client.class);
cfg.addAnnotatedClass(model.Dealer.class);
if(module == "B") {
cfg.addAnnotatedClass(model.Deal.class);
cfg.addAnnotatedClass(model.Dealer.class);
cfg.addAnnotatedClass(model.Product.class);
cfg.addAnnotatedClass(model.Provider.class);
}
return cfg.buildSessionFactory();
}catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
如您所见,我可以部分地按模块划分映射。但是有一个我无法解决的细节: 当我在模块A的客户端上安装时,我不希望休眠状态创建'Deal','Product'和'Provider'表,而仅创建'Client'和'Dealaer'表,因为对于A场景,客户应该只知道与经销商的联系方式。但是,当休眠状态读取Dealer实体时,它会抱怨Deal类未映射。所以我想要的是,当休眠状态读取表时,它将忽略我声明交易的行。如果模块是A,有什么办法可以忽略这些行?
注意:我仅使用一个示例来方便理解。实际情况更复杂。