我正在使用JPA2和Hibernate,并尝试为我的实体引入一个公共基类.到目前为止看起来像这样:
@MappedSuperclass
public abstract class BaseEntity {
@Id
private Long id;
@Override
public int hashCode() {
// ...
}
@Override
public boolean equals(Object obj) {
// ...
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,对于每个表都有一个$entityname_seq我想用作序列生成器的序列.如何从我的子类中设置它?我想我需要覆盖@GeneratedValue并使用@SequenceGenerator创建一个新的SequenceGenerator.
通用实体,超类:
@MappedSuperclass
public abstract class GenericEntity {
private Integer id;
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
}
Run Code Online (Sandbox Code Playgroud)
pojo:
@Entity
@Table(name = "POJO_ONE")
@SequenceGenerator(name = "HB_SEQ_POJO_ONE", sequenceName = "SEQ_POJO_ONE", allocationSize = 1)
public class PojoOne extends GenericEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HB_SEQ_POJO_ONE")
@Column(name = "ID")
@AttributeOverride(name = "id", column = @Column(name = "ID"))
private Integer id;
@Override
public Integer getId() {return id;}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用thoses注释:@AttributeOverride,@ Id,...但它不起作用.你能帮助我吗?我想覆盖属性"id"以通过pojo/table指定另一个列名和序列.最好的方法是什么?
给定以下类结构:
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Animal {}
@Entity
public class Dog {}
@Entity
public class Cat {}
Run Code Online (Sandbox Code Playgroud)
使用Spring Data JPA,是否可以使用通用Animal存储库Animal在运行时持久保存而不知道Animal它是哪种类型?
我知道我可以使用每个实体的存储库并使用instanceof这样的方式来实现:
if (thisAnimal instanceof Dog)
dogRepository.save(thisAnimal);
else if (thisAnimal instanceof Cat)
catRepository.save(thisAnimal);
}
Run Code Online (Sandbox Code Playgroud)
但我不想诉诸于使用的不良做法instanceof.
我试过使用这样的通用存储库:
public interface AnimalRepository extends JpaRepository<Animal, Long> {}
Run Code Online (Sandbox Code Playgroud)
但这导致了这个例外:Not an managed type: class Animal.我猜是因为Animal不是Entity,它是一个MappedSuperclass.
什么是最好的解决方案?
顺便说一句 - Animal列出其余的我的课程persistence.xml,所以这不是问题.
在Symfony2应用程序中,我有一个MainBundle和不同的包,可以启用或不启用.在MainBundle中,我需要拥有模型和基本实体.在OtherBundle的实体具有比相同的表名实体中MainBundle.
MainBundle中的夹具需要加载或不加载MainBundle以外的其他捆绑:
MainBundle
- Model
- Entity (Table name "test")
- Fixtures
OtherBundle
- Entity (Table name "test")
- Fixtures
OtherBundle2
- Entity (Table name="test")
- Fixtures
Run Code Online (Sandbox Code Playgroud)
如果我使用的@ORM\MappedSuperclass的模型,一个@ORM \实体的实体在MainBundle和@ORM \实体在OtherBundle然后Doctrine2停止与错误"表已经存在".
我不能使用继承表,因为我的模型不需要知道其他包中的其他实体.该@ORM\DiscriminatorMap着点OtherBundle.
有没有办法做到这一点 ?
我的数据库结构如下:
工作:
评论:
WorkComment与Work的ManyToOne关系:
@ManyToOne(targetEntity="Work", inversedBy="comments")
Run Code Online (Sandbox Code Playgroud)
Work与WorkComment有一个OneToMany关系:
@OneToMany(targetEntity="WorkComment", mappedBy="work")
Run Code Online (Sandbox Code Playgroud)
问题是Doctrine在更新架构时给了我这个错误:
[Doctrine\ORM\Mapping\MappingException]
It is illegal to put an inverse side one-to-many or many-to-many association on
mapped superclass 'Acme\...\AbstractImageWork#comments'.
Run Code Online (Sandbox Code Playgroud)
我想这与MappedSuperclass 抽象图像工作有关,它位于Work和PhotoWork之间,但我实际上并没有将这种关系放在MappedSuperclass上,而是放在CTI表上.那么为什么Doctrine会这样呢?
有任何想法吗?
relationship class-table-inheritance mappedsuperclass doctrine-orm
我的Web应用程序中有一些域模型类,它们与自己有层次关系.一个示例是用于对用户发布进行分类的分层类别结构.
有一些逻辑与这些类的层次性有关,这是常见的.所以我试图将逻辑移到一个通用的@MappedSuperclass注释超类中.
就像是 :
@MappedSuperclass
public abstract class HierarchicalBaseEntity<N extends HierarchicalBaseEntity<N>>
extends BaseEntity {
@ManyToOne(optional=true)
@JoinColumn(name="parent")
private N parent;
private int depth;
public N getParent() { ...
public void setParent(N newParent) { ...
public boolean isRoot() { ...
public int getDepth() { ...
public boolean isDescendantOf(N ancestor) { ...
public static <N extends HierarchicalBaseEntity<N>> N getCommonAncestor(N a, N b) { ...
public static <N extends HierarchicalBaseEntity<N>> Collection<N> reduceToCommonAncestors(Collection<N> entities) { ...
}
Run Code Online (Sandbox Code Playgroud)
然后子类扩展HierarchicalBaseEntity,将自己作为泛型类型N:
@Entity
public class CategoryBean extends HierarchicalBaseEntity<CategoryBean> { …Run Code Online (Sandbox Code Playgroud) 几乎我们数据库中的每个表都有一个FK到审计表,它记录了创建,更新和删除的状态(日期和用户名).
我们将审计表映射到Auditing类并使用它如下:
@MappedSuperclass
public class BusinessObject extends DataObject {
private static final long serialVersionUID = -1147811010395941150L;
@OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinColumn(name = "AUD_ID")
private AuditingObject auditing;
...
Run Code Online (Sandbox Code Playgroud)
正如您所期望的那样,几乎每个实体都从BusinessObject扩展而来.
有一种简单的方法可以说,对于每个businessObject,只接收"auditing.deleted为null".
我已经尝试在businessObject中添加@Where和@WhereJoinTable,但这似乎不像我期望的那样工作.
目前,我已经对我的一个查询做了这个工作,但我讨厌为所有查询执行此操作,因为我们有大约150个查询.
@NamedQuery(
name="allCountries",
query="SELECT c FROM Country c"
+ " LEFT JOIN FETCH c.labelDefinition "
+ " LEFT JOIN FETCH c.labelDefinition.translations "
+ " WHERE c.auditing.deleted is null"
+ " ORDER BY c.code"
)
Run Code Online (Sandbox Code Playgroud) 当我在线部署包含映射的超类实体的symfony网站时,我收到以下错误:
AnnotationException: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\MappedSuperClass" in class Acme\DemoBundle\Entity\Foo does not exist, or could not be auto-loaded.
Run Code Online (Sandbox Code Playgroud)
最糟糕的是,如果我们使用web/app.php(调试模式为true),则不会显示此错误,而如果您使用web/app_dev.php则会阻止该程序.
我应该补充一点,在本地,使用web/app.php或web/app_dev.php时不会显示此错误.
有没有人对这个黑暗的谜团有所了解?
提前感谢任何提示.
我有一个AbstractEntity类,它由我的应用程序中的所有实体扩展,基本上充当标识符提供者.
@MappedSuperclass
public class AbstractEntity implements DomainEntity {
private static final long serialVersionUID = 1L;
/** This object's id */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="creation_date", nullable = false, updatable=false)
private Date creationDate = new Date();
/**
* @return the id
*/
public long getId() {
return this.id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
我现在有一个案例,我需要为我的几个实体类定义一个单独的Id,因为这些需要有一个自定义的序列生成器.怎么能实现这一目标?
@Entity
@Table(name = "sample_entity")
public class ChildEntity extends …Run Code Online (Sandbox Code Playgroud) 我面临着一个典型的问题。想象一下对象之间典型的 1-N 关系。准确地说,用户(U)和房间(R):[U]*---1[R]。
问题来了,Room 应该是带有实现的抽象基类,例如 BlueRoom、RedRoom。如何正确设置用户实体内的关系?
public interface Room { ... }
@MappedSuperclass
public abstract class RoomSuperclass implements Room { ... }
@Entity
public class BlueRoom extends RoomSuperclass { ... }
@Entity
public class RedRoom extends RoomSuperclass { ... }
@Entity
public class User {
@ManyToOne(targetEntity = ???) // I don't know if it will be BlueRoom or RedRoom
private Room room; // ManyToOne cannot be applied on mapped superclass
}
Run Code Online (Sandbox Code Playgroud)
我知道这可能可以通过在 RoomSuperclass 上使用 @Entity 而不是 @MappedSuperclass 来解决,但我不确定这是否是一个好的解决方案以及是否有更好的解决方案。
mappedsuperclass ×10
java ×5
hibernate ×4
jpa ×4
doctrine-orm ×2
symfony ×2
annotations ×1
doctrine ×1
eclipselink ×1
entity ×1
generics ×1
java-ee ×1
orm ×1
overriding ×1
relationship ×1
sequence ×1
soft-delete ×1
spring ×1