我认为这应该很简单,但我无法弄清楚如何做到这一点.假设我有以下地图:
public class AnimalMap : ClassMap<Animal> { Id( x => x.Id); }
public class CatMap: SubclassMap<Cat> {
Extends<AnimalMap>();
Map(x => x.IsDomestic);
}
Run Code Online (Sandbox Code Playgroud)
这就像我期望的那样创建表:
Animal
------
Id
Cat
----
AnimalId : FK to Animal (named FK3500ABA0D)
IsDomestic
Run Code Online (Sandbox Code Playgroud)
如上所述,FK由db生成,最终为FK3500ABA0D.我想要做的就是设置该约束的名称,但是我找不到如何通过Fluent NHibernate(或者实际上甚至简单的NHibernate)来做到这一点.
那么,我错过了什么?
nhibernate foreign-key-relationship fluent-nhibernate joined-subclass
经过对谷歌的一些研究,我没有找到任何有我问题的人,这就是我在这里发布的原因.在我的应用程序中,我有三个实体:用户(摘要),客户,代理商.客户和代理商扩展用户.这是用户的代码:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class User extends AbstractModel {
@Column(unique = true)
@NotNull
@Email
public String email;
@NotNull
public String password;
}
Run Code Online (Sandbox Code Playgroud)
问题是生成的模式只创建一个包含User,Customer和Agency字段的表,这通常是InheritanceType.SINGLE_TABLE(默认)的行为.
使用Ebean和@Inheritance注释有什么问题吗?我尝试了InheritanceType.TABLE_PER_CLASS,它也没有用.我从来没有使用JPA这个问题.有人可以帮忙吗?
非常感谢 ;)
我不知道什么.我坐在这里有一个解决方案我有1个超类,有2个子类,我现在使用JoinedSubClass映射它,但我得知这个方法已经过时,并说我应该使用ClassMap和SubClassMap,但如果我这样做AutoMapping不起作用,我不希望这样.这有什么解决方法吗?
这是层次结构:
public class Tag : Entity
{
public virtual string Name {get;set;}
public virtual User User {get;set;}
}
public class RespondentTag : Tag
{
public virtual IList<Respondent> Respondents {get;set;}
}
public class ArchiveTag : Tag
{
public virtual IList<Survey> Surveys {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
正如您可能想到的那样,我希望这是一个每个层次结构的表 - 映射与子类的列表是多对多.就像表'Tag',然后是Tag_Respondent和Tag_Archive(用于多对多关系).
这是我目前正在使用的映射:
public class TagMap : IAutoMappingOverride<Tag>
{
public void Override(AutoMapping<Tag> mapping)
{
//This is obsolete
mapping.JoinedSubClass("RespondentTagId", RespondentTagMap.AsJoinedSubClass());
mapping.JoinedSubClass("ArchiveTagId", ArchiveTagMap.AsJoinedSubClass());
}
}
public class RespondentTagMap
{
public static Action<JoinedSubClassPart<RespondentTag>> AsJoinedSubClass()
{
return part =>
part.HasManyToMany(x …Run Code Online (Sandbox Code Playgroud) .net nhibernate fluent-nhibernate automapping joined-subclass
我有一个类设置,看起来像这样:
public abstract class Parent
{
public virtual bool IsDeleted { get; set; }
}
public class Child : Parent
{
}
public class Other
{
public virtual ICollection<Child> Children { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Child被映射为Parent的join-subclass.Childen被映射为多对一包.这个包有一个名为SoftDeletableFilter的过滤器.过滤器映射如下所示:
<filter-def name="SoftDeleteableFilter" condition="(IsDeleted = 0 or IsDeleted is null)" />
Run Code Online (Sandbox Code Playgroud)
问题是当加载Other.Children时,过滤器将应用于Child表而不是父表.有没有办法告诉NHibernate将过滤器应用于父类?
编辑:这是父映射:
<class name="Parent">
<id ..
<property name="IsDeleted" type="System.Boolean">
<column name="IsDeleted" />
</property>
<joined-subclass name="Child">
<key>
<column name="ParentId" />
</key>
...
</joined-subclass>
</class>
Run Code Online (Sandbox Code Playgroud) 考虑Spring Data JPA(+ Hibernate)应用程序中的以下类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "person")
public class Person { }
@Entity
@Table(name = "customer")
public class Customer extends Person { }
@Entity
@Table(name = "employee")
public class Employee extends Person { }
@Entity
@Table(name = "manager")
public class Manager extends Employee { }
public interface IPersonRepository extends JpaRepository<Person, Long> { }
public interface ICustomerRepository extends JpaRepository<Customer, Long> { }
public interface IEmployeeRepository extends JpaRepository<Employee, Long> { }
Run Code Online (Sandbox Code Playgroud)
我最常见的用例涉及调用以下方法(继承自JpaRepository):
IPersonRepository.findAll();
Run Code Online (Sandbox Code Playgroud)
每当调用此方法时,Hibernate都会发出以下SQL查询:
select
person0_.id …Run Code Online (Sandbox Code Playgroud) 这是我的heirarchy:
class abstract Entity { /*members*/ } // mapped to entity table
class abstract User : Entity { /*members*/ } // mapped to user table
class Employee : User { /*no members*/ } // no table, discriminator = "E"
class Contractor : User { /*no members*/ } // no table, discriminator = "C"
这是我在两个单独的hbm文件中的映射:
<class name="Entity" table="entity" xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" column="id">
<generator class="guid.comb" />
</id>
<property ... />
</class>
<joined-subclass name="User" extends="Entity" table="user">
<key column="id" />
<discriminator column="type" />
<property … 几天来,我一直试图围绕子类并在 Fluent nHibernate 中加入子类,但没有取得任何进展。我查看了 wiki,但它似乎没有给我足够的信息,谷歌搜索会返回使用折旧代码或看似无关的旧结果。
我只想要一个简单的例子,如果可能的话
我有四个看起来像这样的表:
+--------------+ +---------------+ +---------------+
| Animal | | AnimalType | | Reptile |
+==============+ +===============+ +===============+
| Id | | Id | | AnimalId |
| AnimalTypeId | | Description | | TongueLength |
| version | +---------------+ +---------------+
+--------------+
Run Code Online (Sandbox Code Playgroud)
所以 Animal 表有一个引用 AnimalType 表的 FK,一个只包含 1 个 Id 的查找表,作为测试,
1 = 爬行动物
这个想法是,AnimalTypeId 列是我的鉴别器列,它拆分了我的子类,并且每个动物表(爬行动物)都有 FK 主键,它们根据其 AnimalTypeId 引用来自 Animal 表的 Id。
所以我的 C# 类看起来像这样
public class Animal
{
public virtual int Id {get;set;} …Run Code Online (Sandbox Code Playgroud) 考虑下面的课程
产品(抽象根类)
@Entity
@Table(name="PRODUCT")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="PRODUCT_TYPE")
public abstract class Product {
@Id
@GeneratedValue
@Column(name = "product_ent_id", insertable = false)
private int productEntId;
}
Run Code Online (Sandbox Code Playgroud)
汽车(扩展产品的另一个父类)
@Entity
@Table(name="CAR")
@DiscriminatorValue(value="Car")
@PrimaryKeyJoinColumn(name="car_ent_id")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="CAR_TYPE")
public abstract class Car extends Product {
@Column(name = "car_ent_id", insertable = false, updatable = false)
private int carEntId;
}
Run Code Online (Sandbox Code Playgroud)
Speeder(扩展Car的层次结构的最后一个孩子)
@Entity
@Table(name="SPEEDER")
@DiscriminatorValue(value="Speeder")
@PrimaryKeyJoinColumn(name="speeder_ent_id")
public class Speeder extends Car {
@Column(name = "speeder_ent_id", insertable = false, updatable = false)
private int speederEntId;
}
Run Code Online (Sandbox Code Playgroud)
当JPA使用鉴别器将我的对象模型映射到我的数据模型时,在单个父对子层次结构中使用鉴别器对我来说很好,鉴别器值被正确设置,但是当我添加另一层次的层次结构时,Speeder-extends- Car-extend-Product …
我有以下代码DisseminationArea作为子cals Feature:
@Entity
@Table(name = "features")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "subtype_id", discriminatorType = DiscriminatorType.INTEGER)
public class Feature {
@Id
@Column(name="id")
@GeneratedValue(generator="sqlite")
@TableGenerator(name="sqlite", table="sqlite_sequence",
pkColumnName="name", valueColumnName="seq",
pkColumnValue="features")
@Getter
@Setter
private long id;
@ManyToOne
@JoinColumn(name = "subtype_id")
@Getter
@Setter
private FeatureSubtype featureSubtype;
@ManyToOne
@JoinColumn(name = "parent_id")
@Getter
@Setter
private Feature parent;
...
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这将实体保存到数据库时会导致异常,因为该subtype_id字段被使用了两次。
我可以以某种方式注释它,以便JPA知道它是同一字段吗?
我有以下 JPA 2.0 实体
@Entity
@Inheritance(strategy= InheritanceType.JOINED)
public abstract class BookKeepingParent implements Serializable {
@Id
protected Long Id;
...
}
@Entity
public class Employee extends BookKeepingParent {
private String name;
@ManyToOne
private Role role;
...
}
@Entity
public class Role extends BookKeepingParent {
private String name;
...
}
Run Code Online (Sandbox Code Playgroud)
我想让 JPA 为我生成表,因为它可以更轻松地在多个位置安装。我通常会期望它产生这个:
CREATE TABLE bookkeepingparent (
id bigint NOT NULL,
dtype character varying(31),
CONSTRAINT bookkeepingparent_pkey PRIMARY KEY (id )
)
CREATE TABLE role (
id bigint NOT NULL,
name character varying(255), …Run Code Online (Sandbox Code Playgroud) 假设以下代码片段使用@PrePersist和@PreUpdate注释以及Joined-type继承:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class A {
...
@PrePersist
private void prePersist() {
...
}
@PreUpdate
private void preUpdate() {
...
}
}
@Entity
@DiscriminatorValue("B")
public class B extends A {
...
@PrePersist
private void prePersist() {
...
}
@PreUpdate
private void preUpdate() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
问题:我们可以依赖回调方法的任何执行顺序吗?
例如,当持久化A类和B类时,B中的prePersist方法在A中的prePersist方法之前执行或反之?
我们可以假设B中的prePersist将在A级持久化之前执行吗?
我的问题与更改保留其 ID 的实体的类型非常相似,但我使用的是 InheritanceType.JOINED 而不是 Table_per_class。
这意味着我不会更改任何表,只是创建一个新的子类,其 ID 与超类相同。
总而言之,我有一个 Person 类和一个 Doctor,它扩展了 Person 并具有相同的 id。我需要从数据库中检索一个 Person 并将其设置为 Doctor,保留 Person 实体中的所有数据,但为 Doctor 实体创建一些额外的数据。
尝试合并医生会生成一个新 ID,这对我无效。
这是我首先尝试过的
private Person getDoctor(Person person) {
// Person already a doctor ==> OK
if (person instanceof Doctor) {
return person;
}
// Transient Person ==> //Transient Doctor OK
if (person == null) {
return new Doctor();
}
// Creates a Doctor from the person (only setting id...),
// and merges it ==>
fails …Run Code Online (Sandbox Code Playgroud) joined-subclass ×12
jpa ×6
inheritance ×5
nhibernate ×4
c# ×3
hibernate ×3
.net ×1
automapping ×1
callback ×1
db-schema ×1
ebean ×1
eclipselink ×1
filter ×1
foreign-keys ×1
hbm ×1
java ×1
sqlite ×1
subclass ×1
subclassing ×1