我不知道实际的数学术语(多对一映射是我使用的术语)
这是我的要求:
hash_code = hash_function(element 1, element 2, ...... element n)
Run Code Online (Sandbox Code Playgroud)
我应该能够检索到
bool b = is_valid_hash(hash_code, element x)
Run Code Online (Sandbox Code Playgroud)
该函数is_valid_hash
应该能够告诉我天气“ element x
”是传入的元素hash_function
这种哈希函数的名称是什么?一个散列应该能够映射到多个元素(而不是冲突)。
假设有一个抽象模型CarOwner:而Person或Business可以是CarOwner.此外,具有特定VIN的汽车可以属于(涉及)个人或企业,但不属于两者(互斥的情况).在下面的代码的最后,我提出了两种可能性(参见代码中的注释"#1.我应该拥有这个???"和"#2.......或者我应该拥有这个???").在第一种可能性中,与抽象模型建立了多对一关系,我不确定这是否是正确的方法.在第二种情况下,建立了两种关系,我不确定这是否正确,特别是不清楚如何使它们相互排斥.那么哪一个是正确的,如果不是,请尽可能提供正确答案.谢谢.
class CarOwner(models.Model):
location = models.CharField(max_length=50, blank=True)
class Meta:
abstract = True
class Person(CarOwner):
name = models.CharField(max_length=50, blank=True)
class Business(CarOwner):
business_id = models.CharField(max_length=50, blank=True)
class Car(models.Model):
vin = models.CharField(max_length=50, blank=True)
# 1. SHOULD I HAVE THIS??? (CarOwner is abstract)
carowner = models.ForeignKey(CarOwner, blank=True, null=True)
# 2. ...OR SHOULD I HAVE THIS???
person = models.ForeignKey(Person, blank=True, null=True)
business = models.ForeignKey(Business, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud) django django-models foreign-key-relationship django-orm many-to-one
我们先来描述一下我的情况.我正在使用Symfony2,我的实体之间的关系有问题.
我有两个链接在一起的实体.这两个实体是AssociationQuestion
和AssociationPossibleAnswer
.我目前正在创建一个问题软件,其中必须将左侧的一个可能答案与右侧的另一个可能答案相关联,例如在以下示例中:
目前,我正计划在类中使用两个属性来AssociationQuestion
保存许多AssociationPossibleAnswer
对象.第一个数组将包含左侧可能的答案,第二个数组将包含右侧可能的答案.
因此,对我来说,看起来我将有两个oneToMany关系 AssociationQuestion
AssociationQuestion:
oneToMany:
possibleAnswersLeft:
targetEntity: AssociationPossibleAnswer
mappedBy: associationQuestion
possibleAnswersRight:
targetEntity: AssociationPossibleAnswer
mappedBy: associationQuestion
Run Code Online (Sandbox Code Playgroud)
然后,在AssociationPossibleAnswer
,我会有一个ManyToOne关系:
AssociationPossibleAnswer:
manyToOne:
associationQuestion:
targetEntity: AssociationQuestion
Run Code Online (Sandbox Code Playgroud)
问题是我在尝试验证我的学说时遇到以下错误.似乎你不能像我希望的那样将两个实体链接到一个......
* The field AssociationQuestion#possibleAnswersLeft is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity AssociationPossibleAnswer#associationQuestion does not contain the required 'inversedBy=possibleAnswersLeft' attribute.
* The field AssociationQuestion#possibleAnswersRight is on the inverse side of a bi-directional relationship, but the specified mappedBy …
Run Code Online (Sandbox Code Playgroud) 我做了一个有更多关系的小应用程序。现在我想删除我的表的详细信息我该如何删除我没有任何要删除的想法。
关系如下:
PanCard-->员工(小野一一)
Employee-->ProjectManger(与Employee的双向多对一关联)
项目-->项目管理器(与项目的双向多对一关联)
现在我想把表数据一一删除
下面是我的 POJO 类代码:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="pName")
private String pName;
@Column(name="pNumber")
private int pNumber;
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="EId")
private Employee employee;
Run Code Online (Sandbox Code Playgroud)
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "empFirstName")
private String empFirstName;
@Column(name = "empLastName")
private String empLastName;
@Column(name = "empDepartment")
private String empDepartment;
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="pmId")
private ProjectManager projectManager;
Run Code Online (Sandbox Code Playgroud)
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String department;
private String managerFirstName;
private String managerLastName;
//bi-directional many-to-one association …
Run Code Online (Sandbox Code Playgroud) 有人可以解释一下为什么在 @ManyToOne 关系上使用 @Filter 不起作用吗?这里我有一个非常简单的例子来展示这一点:
我在数据库中创建了两个简单的表(foo 和 bar)
Foo: id/id_bar
bar: id/name/state (state can be active or inactive)
Run Code Online (Sandbox Code Playgroud)
和我的实体:
@Entity
@FilterDef(name = "foo_active")
@Table(name = "foo")
public class Foo extends AbstractTimestampEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
private Integer id;
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
@JoinColumn(name = "bar")
@Filter(name = "foo_active", condition = "state='active'")
private Bar bar;
}
@Entity
@Table(name = "foo")
public class Bar extends AbstractTimestampEntity implements Serializable {
private …
Run Code Online (Sandbox Code Playgroud) 我正在使用JPA 2.1和Hibernate 4.3.7
我试图调整我的应用程序,以便将关系转为懒惰并仅获取我需要的东西
我对多对一关系有问题,当我再次加载实体时转为惰性时,即使获取了该实体并且该代理在的视图部分(JSF)中不起作用,Hibernate也用代理替换了该实体应用程序。当多对一处于急切模式但休眠时,即使我不需要多对一,也要对每个多对一执行更多选择,该问题消失了
@Entity
public class Department {
@Id
private Integer id;
//...
}
Run Code Online (Sandbox Code Playgroud)
1 /
@Entity
public class Employee {
@Id
private Integer id;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "id_department", referencedColumnName = "id")
private Department department;
//...
}
Run Code Online (Sandbox Code Playgroud)
JPQL查询:
SELECT e FROM Employee e LEFT JOIN FETCH e.department WHERE e.id=:id
Run Code Online (Sandbox Code Playgroud)
=>一个选择查询=>更快,但是Department的类型为Department _ $$ _ jvst3ac_5f(employee.getDepartment()。getClass()。getCanonicalName()),并且此代理在应用程序的视图部分中不起作用
2 /
@Entity
public class Employee {
@Id
private Integer id;
@ManyToOne(fetch = FetchType.EAGER, optional = true)
@JoinColumn(name = …
Run Code Online (Sandbox Code Playgroud) I'm having problems when saving entities in my DB.
I have something like (very simplified) :
@Entity
public class Building {
@OneToMany(mappedBy = "building", fetch = FetchType.EAGER)
private List<Employee> employees;
}
@Entity
public class Employee {
@NotNull
@ManyToOne
@JoinFetch(INNER)
@JoinColumn
private Building building;
@PostLoad
private void onLoad(){
if (this.plannedOrder == null) {
//For old entities in this DB, update plannedOrder if null
if (this.order < FIRST.getCode()) {
this.plannedOrder = FIRST;
} else if (this.order >= FIRST.getCode() && this.order < SECOND.getCode()) …
Run Code Online (Sandbox Code Playgroud) 这个问题的变化已被要求,并回答了很多次,答案有很多重叠的细节.我已经尝试过这些答案提出的许多不同的事情,但是没有一个在我的案例中有效.
我有一个带有父表和子表的SQLite数据库.这是一个非常简单的设置.我正在使用NHibernate 4.0.4进行代码映射而不是流畅,因为我建议前者是新的,而后者是改进的.
实体:
public class BillingItem
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
// ... other properties
public virtual ICollection<PaymentItem> PaymentItems { get; set; }
public BillingItem()
{
PaymentItems = new List<PaymentItem>();
}
}
public class PaymentItem
{
public virtual int ID { get; set; }
public virtual BillingItem OwningBillingItem { get; set; }
// ... other properties
}
Run Code Online (Sandbox Code Playgroud)
BillingItem映射:
public class BillingItemMapping : ClassMapping<BillingItem> …
Run Code Online (Sandbox Code Playgroud) Posts Model
/*set many to one relation with Privacy_Level model*/
@ManyToOne//(fetch = FetchType.LAZY)
@JoinColumn(name = "privacy_level_id",referencedColumnName = "id", insertable = false, updatable = false)
public Privacy_Level privacy_level_t;
-------------------------------------------------------------------------------------------------------------
Privacy_Level Model
/*set one to many relation with Posts model*/
@OneToMany(mappedBy = "privacy_level_t")
public List<Posts> posts;
/*set many to one relation with Table_Status model*/
@ManyToOne
@JoinColumn(name = "status",referencedColumnName = "id", insertable = false, updatable = false)
public Table_Status table_status;
--------------------------------------------------------------------------------------------------------------
Table_Status Model
/*set many to one relation with Privacy_level table*/
@OneToMany(mappedBy = …
Run Code Online (Sandbox Code Playgroud) one-to-many many-to-one playframework ebean playframework-2.6
再会。我希望用不同表中的信息来注释我的模型。
class CompetitionTeam(models.Model):
competition_id = models.ForeignKey('Competition', on_delete=models.CASCADE, to_field='id', db_column='competition_id')
team_id = models.ForeignKey('Team', on_delete=models.CASCADE, to_field='id', null=True, db_column='team_id')
...
class Team(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30)
teamleader_id = models.ForeignKey('User', on_delete=models.CASCADE, to_field='id', db_column='teamleader_id')
...
class Competition(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30)
...
Run Code Online (Sandbox Code Playgroud)
循环浏览我的比赛,我希望检索要显示的竞赛团队对象列表以及相关团队的名称。我试过:
CompetitionTeam.objects.filter(competition_id=_competition.id).filter(team_id__in=joined_team_ids).annotate(name=...)
Run Code Online (Sandbox Code Playgroud)
- 我在其中放置子查询表达式而不是省略号。但是,我不确定如何匹配team_id变量。例如。
*.anotate(name=Subquery(Team.objects.filter(id=competitionteam.team_id)).values('name'))
Run Code Online (Sandbox Code Playgroud)
相关问题是:Django annotate field value from another model,但我不确定在这种情况下如何实现。在这种情况下,mymodel_id
我使用 代替 ,team_id
但它只有来自 Team 对象的参数,而不是我的竞赛团队对象。我不太明白OuterRef
,但这是我失败的尝试:
CompetitionTeam.objects.filter(competition_id=_competition.id).filter(team_id__in=joined_team_ids).annotate(name=Subquery(Team.objects.get(id=OuterRef('team_id'))))
"Error: This queryset contains a reference to an outer query and may …
Run Code Online (Sandbox Code Playgroud) many-to-one ×10
jpa ×4
hibernate ×3
java ×3
one-to-many ×3
django ×2
.net ×1
c# ×1
cascade ×1
django-orm ×1
doctrine-orm ×1
ebean ×1
eclipselink ×1
filter ×1
hash ×1
lazy-loading ×1
nhibernate ×1
one-to-one ×1
php ×1
symfony ×1