我有以下简单的设计:
有效的XHTML http://www.kawoolutions.com/media/persons-roles-reduced.png
这基本上是JPA不支持的非不相交继承的解决方法.人可以自己存在,但可以选择单个玩家实体,单个教练实体或两个实体来完成逻辑.
以下是相当容易理解的JPA 2.0映射(注意@Id前向关联):
@Entity
@Table(name = "Persons")
public class Person implements Serializable
{
@Id
@Column(name = "id")
private Integer id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@OneToOne(mappedBy = "person")
private Coach coach = null;
@OneToOne(mappedBy = "person")
private Player player = null;
...
}
@Entity
@Table(name = "Players")
public class Player implements Serializable
{
@Column(name = "registration_nbr")
private String registrationNbr = null;
@Id
@OneToOne
@JoinColumn(name = "id")
private Person …Run Code Online (Sandbox Code Playgroud) 有两种型号:
车型/ User.java
@Entity
@Table(name="users")
public class User extends Model
{
@Id
public int user_id;
public String firstName;
public String lastName;
@OneToOne
@PrimaryKeyJoinColumn
public UserProfile profile;
public static Finder<Integer,User> find = new Finder<Integer,User>( Integer.class, User.class );
}
Run Code Online (Sandbox Code Playgroud)
车型/ UserProfile.java
@Entity
@Table(name="user_profiles")
public class UserProfile extends Model
{
@Id
public int user_id;
public String bio;
@OneToOne(mappedBy = "user_id")
public User user;
public static Finder<Integer,UserProfile> find = new Finder<Integer,UserProfile>( Integer.class, UserProfile.class );
}
Run Code Online (Sandbox Code Playgroud)
一些数据:
INSERT INTO users VALUES(1,"Joe","Bloh");
INSERT INTO users VALUES(2,"Maria","Luis"); …Run Code Online (Sandbox Code Playgroud) 我有两张表如下:
CREATE TABLE pets(
id int NOT NULL AUTO_INCREMENT,
user_id int,
any_data varchar(255),
foreign key (user_id) references users(id),
primary key(`id`)
);
CREATE TABLE users(
id int NOT NULL AUTO_INCREMENT,
pet_id int,
any_data varchar(255),
foreign key (pet_id) references pets(id),
primary key(`id`)
);
Run Code Online (Sandbox Code Playgroud)
我的模型有下一个:
用户:
public function relatedPet() {
return $this->hasOne("Pet", "pet_id");
}
Run Code Online (Sandbox Code Playgroud)
宠物:
public function relatedUser() {
return $this->belongsTo("User", "user_id ");
}
Run Code Online (Sandbox Code Playgroud)
我想保存用户并与现有宠物有关,但我不知道该怎么做:
$user= new User(array("any_data"=>"Hi, this is a test"));
$pet = Pet::find(1);
Run Code Online (Sandbox Code Playgroud)
如何创建两个objets之间的关系?
我有两个模型:1.课程.2.费用.
一门课程只收一费.虽然提供输入完全没问题但是当我试图访问费用数据时,它在费用列中没有显示任何内容.我该如何解决?
课程模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $table='courses';
protected $fillable = ['name'];
public function fee(){
return $this->belongsTo('App\Fee');
}
}
Run Code Online (Sandbox Code Playgroud)
费用型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Fee extends Model
{
protected $table='fees';
protected $fillable = ['fee','course_id'];
public function course()
{
return $this->hasOne('App\Course');
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public function listofCourse(){
$course=\App\Course::all();
return view('listofCourse',compact('course'));
}
Run Code Online (Sandbox Code Playgroud)
查看页面:
<html>
<head>
<title> Course Details </title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<div class="container">
<h3> …Run Code Online (Sandbox Code Playgroud) 假设一个名为 Student 的类/实体,其中包含 id、父亲的职业和母亲的职业,实体类如下所示
学生班
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, unique = true, updatable = false)
private int id;
@OneToOne
@JoinColumn(name = "occupationId")
Occupation fathersOccupation;
@OneToOne
@JoinColumn(name = "occupationId")
Occupation mothersOccupation;
}
Run Code Online (Sandbox Code Playgroud)
和职业类别
@Entity
@Table(name = "occupation")
public class Occupation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "occupationId")
int occupationId;
@Column
String title;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试@OneToOne映射父亲的职业和母亲的职业时,它抛出异常Repeated column in mapping for entity。我尝试添加@Column(name="<columnName>"),但这是不允许的。我真的想要这两个字段,fathersOccupation并在学生表中进行映射mothersOccupation。 …
我正在构建一个基于 django 的应用程序,它从客户端计算机(例如内存)接收一些信息并将其保存到数据库中。
为此,我创建了以下模型:
class Machine(models.Model):
uuid = models.UUIDField('UUID', primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField('Name', max_length=256)
memory = models.OneToOneField('Memory', on_delete=models.CASCADE, null=True)
def save_data(self, data):
if not self.memory:
memory = Memory()
memory.save()
self.memory = memory
self.save()
self.memory.total = data['memory_total']
self.memory.used = data['memory_used']
self.memory.cached = data['memory_total']
self.memory.save()
Run Code Online (Sandbox Code Playgroud)
现在对于内存,我有以下模型:
class Memory(models.Model):
total = models.IntegerField(default=0)
used = models.IntegerField(default=0)
cached = models.IntegerField(default=0)
Run Code Online (Sandbox Code Playgroud)
为了在我从客户端收到数据时将数据保存到机器模型,我调用了 save_data() 方法。在那里我测试是否已经有一个 self.memory 对象,如果没有,我在添加数据之前先创建它。
现在,即使它按预期工作,我想知道是否有更好,更干净的方法来实现这一目标。是否可以将我所有的 OneToOne-Fields 初始化为引用类型的空实例,这样我就不需要每次都这样做?
在Python 3+中使用Pandas处理数据。似乎应该有一种简单的方法来检查两个列是否具有一对一的关系(无论列类型如何),但是我一直在努力寻找实现此目的的最佳方法。
预期输出示例:
A B C
0 'a' 'apple'
1 'b' 'banana'
2 'c' 'apple'
Run Code Online (Sandbox Code Playgroud)
A和B是一对一的吗?真正
A和C是一对一的吗?假
B&C是一对一的吗?假
我有一个数据库架构,其中包括 3 个表 - 用户、客户和卖家。用户表是客户和卖家的通用表。我想在 typeorm 上映射 OneToOne 关系,其中客户和卖家的 FK 是自己的主键。所以他们的id应该与相应的用户id相同。
相当于 JAVA JPA Hibernate 上的 @MapsId 的东西。
提前致谢..
尝试在单个表单上显示我的用户和配置文件模型时尝试获取*fields_for*以产生输出时遇到了一些麻烦,这些使用*has_one*和*belongs_to*关系.
所以这里是模型类顶部的提取:
class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
class Profile < ActiveRecord::Base
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
控制器非常简单和标准:
def new
@user = User.new
end
def edit
@user = User.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
以下是目前的视图中的一个片段:
<%= form_for(@user) do |f| %>
<% f.fields_for :profile do |profile_form| %>
<div class="field">
<%= profile_form.label :name %><br />
<%= profile_form.text_field :name %>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我尝试过其他的东西,比如:
<% fields_for …Run Code Online (Sandbox Code Playgroud) 我在Child模型中创建了一个OneToOneField(父)related_name='children'.在我的观点中,我曾经select_related得到了查询集.但在我的页面中,与父项关联的子项列表显示为空.
Models.py:
class Parent(models.Model):
item = models.CharField(max_length=20)
class Child(models.Model):
parent = models.OneToOneField(Parent, unique = True, related_name = 'children')
price = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
views.py:
def live_prices(request):
parent_queryset = Parent.objects.all().select_related('children')
return render(request, 'live_prices.html', 'parent_queryset' : parent_queryset)
Run Code Online (Sandbox Code Playgroud)
模板:
{% for parent in parent_queryset %}
{% child in parent.children.all %}
{{ child.price }}
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud) django django-models one-to-one django-views django-select-related
one-to-one ×10
django ×2
eloquent ×2
hibernate ×2
jpa ×2
laravel ×2
django-views ×1
ebean ×1
fields-for ×1
has-one ×1
java ×1
laravel-5.2 ×1
mapping ×1
model ×1
node.js ×1
orm ×1
pandas ×1
php ×1
python ×1
typeorm ×1