有没有办法使用MySQL Workbench建模表继承?
我希望ERD看起来与此图像的左侧相似:

我正在研究一个非常大的Rails应用程序.我们最初并没有使用太多的继承,但是我们从顾问那里获得了一些开眼界的经验,并且正在寻求重构我们的一些模型.
我们的应用程序中有很多以下模式:
class Project < ActiveRecord::Base
has_many :graph_settings
end
class GraphType < ActiveRecord::Base
has_many :graph_settings
#graph type specific settings (units, labels, etc) stored in DB and very infrequently updated.
end
class GraphSetting < ActiveRecord::Base
belongs_to :graph_type
belongs_to :project
# Project implementation of graph type specific settings (y_min, y_max) also stored in db.
end
Run Code Online (Sandbox Code Playgroud)
这也导致视图,帮助器和GraphSetting模型本身中的大量条件.这些都不好.
一个简单的重构,我们摆脱GraphType,支持使用更像这样的结构:
class Graph < ActiveRecord::Base
belongs_to :project
# Generic methods and settings
end
class SpecificGraph < Graph
# Default methods and settings hard coded
# Project …Run Code Online (Sandbox Code Playgroud) 我在 Doctrine2 中使用单表继承来存储多个服务的 OAuth 凭据。我想使用服务的 id 作为主键;然而,这并不是所有服务中唯一的。
我已经将数据库设置为使用鉴别器列和服务的 id 作为主键,但是我找不到一种方法让 Doctrine 使用鉴别器列作为键(除了鉴别器列)。我正在使用 docblock 注释,如果我将鉴别器列添加为 @Id 字段,则会出现错误:
Duplicate definition of column...in a field or discriminator column mapping.
Run Code Online (Sandbox Code Playgroud)
如果我只将该字段定义为鉴别器列,则任何重叠的服务 ID 都会更新所有匹配的行。
无论如何要完成这项工作,除了使用自动生成的 it 值?
我正在使用Hibernate 3.2.2 GA和HSQLDB 2.0 GA,我有一个类似于以下的类层次结构:
@Entity
@Table(name = "A_TABLE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(value = "case when CODE IN (1, 2, 3, 4) then 'TypeB'
when CODE IN (5, 6, 7, 8) then 'TypeC' else NULL end")
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
public abstract class A{
(...)
}
@Entity
@DiscriminatorValue("TypeB")
public class B extends A {
(...)
}
@Entity
@DiscriminatorValue("TypeC")
public class C extends A {
(...)
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试执行以下HQL查询,该查询返回B和C类中的对象.
String hql = "from A a where a.someAttr = 3";
Query …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我现在好几天都无法解决。我阅读了很多文档,搜索了很多论坛,但没有找到解决方案。我继承了类,如下所示:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "nc_linktype", discriminatorType = DiscriminatorType.STRING)
@Table(name = "mk_newsletter_coupons")
@DiscriminatorOptions(force = true)
public class BaseNewsletterCoupon extends BaseEntity {...}
@Entity
@DiscriminatorValue("expire")
public class NewsletterCouponsExpire extends BaseNewsletterCoupon {
@Entity
@DiscriminatorValue("region")
public class NewsletterCouponsRegion extends BaseNewsletterCoupon {
Run Code Online (Sandbox Code Playgroud)
我想在 OneToMany 现实的多方面使用这些特定实体:
@Entity(name = "newsletter")
@Table(name = "mk_newsletters")
@Configurable
public class NewsLetter extends BasePictureEntity {
@OneToMany(mappedBy = "newsletter", cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.LAZY)
@IndexColumn(name = "nc_index")
@OrderColumn(name = "nc_index")
@OrderBy("index")
List<NewsletterCouponsExpire> expireCoupons = new …Run Code Online (Sandbox Code Playgroud) hibernate jpa one-to-many single-table-inheritance discriminator
我们对应用程序中的每个表使用单表继承.这允许同一应用程序堆栈的不同实例与相同的DAO一起工作,而它们的实体可能略有不同,可能包含该实例特有的信息.抽象类定义基本表结构,如果该实例需要,扩展定义其他列:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "client")
public abstract class Client extends AbstractPersistable<Long> {
// ...
}
Run Code Online (Sandbox Code Playgroud)
申请A:
@Entity
public class ClientSimple extends Client {
private String name;
// getter, setter
}
Run Code Online (Sandbox Code Playgroud)
申请B:
@Entity
public class ClientAdvanced extends Client {
private String description;
// getter, setter
}
Run Code Online (Sandbox Code Playgroud)
现在DAO可以处理Client应用程序A和B的对象,但是应用程序B可以为其客户端对象定义其他信息,这些信息可以由应用程序B特有的管理器方法读取:
申请A:
Client client = new ClientSimple();
clientDao.save(client);
Run Code Online (Sandbox Code Playgroud)
申请B:
Client client = new ClientAdvanced();
clientDao.save(client);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这意味着每个表中都有一个DTYPE列(或者我可能选择的任何其他名称).有没有办法摆脱这个?我们不需要它,它正在耗尽数据库空间......
谢谢!
编辑
重要提示:@MappedSuperclass不起作用.我们使用QueryDSL作为我们的HQL抽象层.这需要为类型保存查询自动生成查询类型类.但是,只有在使用注释抽象类时才会正确生成这些@Entity.
这是必要的,因为我们想Client在实际查询ClientSimple应用程序A和 …
java inheritance hibernate single-table-inheritance querydsl
我正在使用Rails 4.0.1应用程序Capybara,FactoryGirl但我无法让我的测试正常工作.
我正在使用单表继承来创建一个Collection < ActiveRecord::Base和一个VideoCollection < Collection模型.在我的测试中使用Factory Girl时,模型似乎没有像他们应该那样实例化.
print page.html在相同视图的测试中运行时,该集合不会出现在页面代码中.rails c test并执行FactoryGirl.create(:video_collection),那么视频集合将被插入到数据库中没有问题.我的模特:
# ------in app/models/collection.rb------
class Collection < ActiveRecord::Base
end
# ------in app/models/video_collection.rb------
class VideoCollection < Collection
end
# ------in db/schema.rb------
create_table "collections", force: true do |t|
t.string "type"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
t.string "tile_image_link"
end
Run Code Online (Sandbox Code Playgroud)
我的工厂:
FactoryGirl.define do
...
factory :collection do |collection|
factory :video_collection, class: …Run Code Online (Sandbox Code Playgroud) 我有以下模型结构:a Banner embeds_many Slides和eachSlide embeds_many contents
class Banner
include Mongoid::Document
embeds_many :slides
accepts_nested_attributes_for :slides, allow_destroy: true
end
class Slide
include Mongoid::Document
embedded_in :banner
embeds_many :contents
accepts_nested_attributes_for :contents, allow_destroy: true
end
class Content
include Mongoid::Document
embedded_in :slide
field :value, type: String
end
Run Code Online (Sandbox Code Playgroud)
我开始使用一张带有一些内容的幻灯片的横幅.现在我向服务器发送一个JSON请求,将新内容添加到现有幻灯片中,并创建包含其内容的新幻灯片; 就像是
'banner' => {
'_id' => '123',
'slides_attributes' => [
{
'_id' => '1',
'contents_attributes' => [
{ '_id' => '1', 'value' => 'some persisted value' },
{ 'value' => 'new content here, there is no …Run Code Online (Sandbox Code Playgroud) single-table-inheritance nested-attributes mongoid4 ruby-on-rails-4.1
我在 Rails 4 中有以下使用单表继承 (STI) 的模型:
class User < ActiveRecord::Base
has_many :notes
end
class Item < ActiveRecord::Base
end
#inherits from Item
class Folder < Item
belongs_to :folder
end
#inherits from Item
class Note < Item
belongs_to :folder
end
Run Code Online (Sandbox Code Playgroud)
我想执行以下查询,该查询应该提取笔记的 id 及其所属文件夹的名称。
user = User.first
user.notes.includes(:folder).pluck(:id, :'folders.name').first
Run Code Online (Sandbox Code Playgroud)
这将返回以下错误:
ERROR: missing FROM-clause entry for table "folders"
Run Code Online (Sandbox Code Playgroud)
由于笔记和文件夹都继承自 Item,因此只有一个项目表,我无法从包含文件夹中提取它。
如果我做:
user = User.first
user.notes.includes(:folder).pluck(:id, :'items.name').first
Run Code Online (Sandbox Code Playgroud)
它返回笔记的名称,而不是所属的文件夹(因为笔记和文件夹都继承自 Item 并且都在项目表中)。有没有办法从包含的文件夹而不是笔记中提取?
我正在使用单表继承策略。我想通过使用鉴别器类型过滤数据库来在数据库中执行搜索。我如何在 JPA 中编写一个函数来执行此操作。
使用 method 定义方法的正常方式findBy...不会产生结果。
这是我的家长课
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="Leave_Type",
discriminatorType=DiscriminatorType.STRING
)
public class LeaveQuota {
// the fields and the required methods
}
Run Code Online (Sandbox Code Playgroud)
这是两个子实体
@Entity
@DiscriminatorValue("Annual")
public class AnnualLeave extends LeaveQuota {
// the fields and the required methods
}
@Entity
@DiscriminatorValue("Casual")
public class CasualLeave extends LeaveQuota {
// the fields and the required methods
}
Run Code Online (Sandbox Code Playgroud)
我想通过分别过滤年假和休闲假来查询数据库。这意味着当我搜索年假时,应检索鉴别器列中值为“annual”的所有记录。我怎样才能实现这个。提前致谢!
inheritance hibernate single-table-inheritance spring-data-jpa spring-boot
hibernate ×4
inheritance ×4
java ×2
doctrine-orm ×1
erd ×1
factory-bot ×1
hsqldb ×1
jpa ×1
mongoid4 ×1
one-to-many ×1
oop ×1
orm ×1
php ×1
querydsl ×1
rspec ×1
ruby ×1
spring-boot ×1
sti ×1