SecondaryTable without DiscriminatorValue



主表:

@Entity()
@Table(name = "USERS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="X__TYPE", discriminatorType=DiscriminatorType.STRING)
public class UsersEntity {
@Id
@Column(name = "USER_ID", length = 15, nullable = false)  
private String userId;
}

项目公共表:

@Entity()
@Table(name = "CUSTOMER_USER")
@SecondaryTable( name = "CUSTOMER_USER", pkJoinColumns = @PrimaryKeyJoinColumn(name="CUSTOMER_USER_ID", referencedColumnName = "USER_ID"))
public class CustomerUserEntity extends UsersEntity {
...
}

项目自定义类型…:

@Entity
@DiscriminatorValue("TYPEA")
public class TypeAEntity extends CustomerUserEntity { 
}
@Entity
@DiscriminatorValue("TYPEB")
public class TypeBEntity extends CustomerUserEntity {
}

可选customerUser = customerUserRepository.findById(" bd6uxlm8001 ");

问题如下:

如何从选择中删除"CustomerUserEntity">

?
select
customerus0_.user_id as user_id2_37_0_,
...
customerus0_1_.active as active1_5_0_,
...        
from
users customerus0_ 
left outer join
customer_user customerus0_1_ 
on customerus0_.user_id=customerus0_1_.customer_user_id 
where
customerus0_.user_id=? 
and customerus0_.x__type in (
'CustomerUserEntity', 'TYPEA', 'TYPEB'
)

解决方案:我将SecondaryTable从CustomerEntity移到UsersEntity,并将CustomerEntity更改为Embeddable。

UserEntity:

@Entity()
@Table(name = "USERS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="X__TYPE", discriminatorType=DiscriminatorType.STRING)
@SecondaryTable( name = "CUSTOMER_USER", pkJoinColumns = @PrimaryKeyJoinColumn(name="CUSTOMER_USER_ID"))
public class UsersEntity {

@Id
@Column(name = "USER_ID", length = 15, nullable = false)
@NotNull
private String userId; 
...
@Embedded 
public CustomerUserEntity customerUserEntity;
}

CustomerUserEntity:

@Embeddable
public class CustomerUserEntity {

@Column(name = "CUSTOMER", length = 35, nullable = false, table = "CUSTOMER_USER")
@Size(max = 35)
@ColumnDetailedInformation(columnLabel = "Ügyfél")
@NotNull
private String customer;
...
}

和TypeAEntity, TypeBEntity

@Entity
@DiscriminatorValue("TYPEA")
public class TypeAEntity extends UsersEntity { 
}
@Entity
@DiscriminatorValue("TYPEB")
public class TypeBEntity extends UsersEntity {
}

我很难过,因为我不想修改公共实体(UserEntity)。

Köszi Csabi提示!:)

天哪!解决方案:

@Entity()
@Table(name = "CUSTOMER_USER")
@SecondaryTable( name = "CUSTOMER_USER", pkJoinColumns = @PrimaryKeyJoinColumn(name="CUSTOMER_USER_ID", referencedColumnName = "USER_ID"))
@DiscriminatorValue("null")
public class CustomerUserEntity extends UsersEntity {
}

并选择:

...
where
customerus0_.user_id=? 
and (
customerus0_.x__type is null 
or customerus0_.x__type in (
'TYPEA', 'TYPEB'
)
)

Example6:https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/domain/inheritance.html

相关内容

  • 没有找到相关文章

最新更新