我有两个应用程序,“account”和“myapp”。我试图在视图中仅显示那些与 request.user 属于同一组织的教师对象。
帐户/models.pyfrom django.contrib.auth.models import User
class Organisation(models.Model):
name = models.CharField(max_length=100, unique=True)
is_active = models.BooleanField(default=True)
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
organisation = models.ForeignKey(Organisation, editable=False)
is_organisationadmin = models.BooleanField(default=False)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
Run Code Online (Sandbox Code Playgroud)
请注意此博客文章中的最后一行,它允许通过以下方式访问用户个人资料信息user.profile.organisation
from django.contrib.auth.models import User
class Teacher(models.Model):
user = models.OneToOneField(User, related_name='teacher')
Run Code Online (Sandbox Code Playgroud)
myapp/views.py
from myproject.account.models import Organisation, UserProfile
from myproject.myapp.models import Teacher
from django.contrib.auth.models import User
def homepage(request):
if request.user.is_authenticated():
teachers = Teacher.objects.filter(user.profile.organisation == request.user.profile.organisation, user__is_active = True)
Run Code Online (Sandbox Code Playgroud)
我收到“/homepage/ 处的 NameError,未定义全局名称“用户””。我认为这是因为我没有正确访问每个 Teacher 对象的 …
我想将外部策略生成器用于我班级中的一个关键属性.最简单的方法如下所示:http: //blog.eyallupu.com/2011/01/hibernatejpa-identity-generators.html 或 http://www.coderanch.com/t/219256/ORM/databases/HBM-Annotation -Foreign-Generator 或 http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example-annotation/
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;
...
...
...
@GenericGenerator(
name = "myForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "osoba"))
@Id
@GeneratedValue(generator = "myForeignGenerator")
@Column(name = "IdPracownik", unique = true, nullable = false)
public int getIdPracownik() {
return this.idPracownik;
}
Run Code Online (Sandbox Code Playgroud)
和Eclipse显示错误:
"类型不匹配:无法从参数转换为注释",下划线"@Parameter"注释.
这是为什么?
我有一个PersonProfile类和一个Person类.在类Person中,我与PersonProfile有一个关系OneToOne.我如何序列化?
class PersonProfile(models.Model):
interests = models.CharField(max_length=200, blank=True)
researchLines = models.CharField(max_length=200, blank=True)
loginName = models.CharField(max_length=50, blank=True)
class Person(models.Model):
profile = models.OneToOneField(PersonProfile)
enviroment = models.ForeignKey(Enviroment, related_name="persons")
Run Code Online (Sandbox Code Playgroud) 我正在休眠状态下锻炼,但自实施几个小时以来一直失败。我在stackoverflow上阅读了很多答案,并经历了2个教程(mkyong和其他一些家伙),但是我不明白我在做什么。
我尝试了很多事情,但最终总是遇到相同的异常(org.hibernate.AnnotationException: Unknown mappedBy in: ch.myapp.model.Employee.office, referenced property unknown: ch.myapp.model.Office.EMPLOYEES)
如果有人能给我提示问题出在哪里,我将非常高兴。
我正在使用此数据库方案,并且尝试在Office和Employee之间实现1:1(我知道这没有多大意义)。
员工班
@Entity
@Table(name = "EMPLOYEES")
public class Employee {
@Id
@GeneratedValue
@Column(name = "EMPLOYEENUMBER")
private Integer employeeNumber;
@Column(name = "FIRSTNAME")
private String firstName;
@Column(name = "LASTNAME")
private String lastName;
@Column(name = "EXTENSION")
private String extension;
@Column(name = "EMAIL")
private String email;
@Column(name = "JOBTITLE")
private String jobTitle;
@Column(name = "OFFICECODE")
private String officeCode;
// ACHTUNG, Test "EmployeeDataAccesTest -> loadEmployee()" greift auf einen
// Null-Wert …Run Code Online (Sandbox Code Playgroud) 我在用户和学生之间的 Symfony 中有一个 OneToOne 关系,其中用户是所有者,当我尝试坚持更改时,双方都没有进行更改,这是我的最后一个配置:
<?php
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="BackendBundle\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Student", mappedBy="user")
*/
private $student;
public function __construct() {
}
/**
* Set student
*
* @param \BackendBundle\Entity\Student $student
*
* @return User
*/
public function setStudent(\BackendBundle\Entity\Student $student)
{
$this->student = $student;
return $this;
}
/**
* Get student
*
* @return …Run Code Online (Sandbox Code Playgroud) 当我添加带有 id 的职位查询,将它们添加到 Employee 实体中设置的职位并保存 Employee 实体时,出现此错误
2020-02-29 18:01:53.689 WARN 18280 --- [nio-8083-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError]
2020-02-29 18:01:53.692 WARN 18280 --- [nio-8083-exec-2] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@52efd6dc<rs=HikariProxyResultSet@690866176 wrapping Result set representing update count of 2>
2020-02-29 18:01:53.692 WARN 18280 --- [nio-8083-exec-2] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@4d7d18da<rs=HikariProxyResultSet@2035306242 wrapping Result set representing update count of -1>....
.....
Run Code Online (Sandbox Code Playgroud)
员工实体
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.LAZY)
@Fetch(value= …Run Code Online (Sandbox Code Playgroud) 什么时候应该使用一对一的关系?什么时候应该添加新字段?什么时候应该将它们分成新表?
在我看来,只要你对字段进行分组和/或该组往往是可选的,你就会使用它.是?
我正在尝试为一个对象创建表,但是分组/分离一切都需要我大约20个连接,有些甚至是4个级别.
难道我做错了什么?我该如何改进?
我想在EF中创建一对一关系,其中外键可以为null(因此,它可以被称为0..1到0..1)
public class ProductInstance
{
public int Id { get; set; }
public int SaleId { get; set; }
public Sale Sale { get; set; }
}
public class Sale
{
public int Id { get; set; }
public ProductInstance ProductInstance { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
FluentAPI 组态:
modelBuilder.Entity<Sale>()
.HasOptional(x => x.ProductInstance)
.WithOptionalPrincipal(x => x.Sale);
Run Code Online (Sandbox Code Playgroud)
但它在表中生成两列ProductInstance:
Sale_id - 外键SaleId这是生成的迁移代码:
CreateTable(
"dbo.ProductInstances",
c => new
{
Id = c.Int(nullable: false, identity: true),
SaleId = c.Int(nullable: false),
Sale_Id …Run Code Online (Sandbox Code Playgroud) 我有一对一的多态,我想找出更新现有关系的最佳方式。
class Image extends Model
{
/**
* Get the owning imageable model.
*/
public function imageable()
{
return $this->morphTo();
}
}
class Post extends Model
{
/**
* Get the post's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
class User extends Model
{
/**
* Get the user's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
Run Code Online (Sandbox Code Playgroud)
PostController 中的更新方法
public function update(Request $request, $id)
{
$post = Post::findOrFail($id);
$post->image()->delete();
$post->image()->save(new Image([
'url'=> $request->input('image_url), …Run Code Online (Sandbox Code Playgroud) one-to-one ×9
hibernate ×3
django ×2
mysql ×2
annotations ×1
c# ×1
database ×1
doctrine ×1
foreign-keys ×1
java ×1
join ×1
jpa ×1
laravel ×1
nameerror ×1
persistent ×1
polymorphism ×1
relationship ×1
spring ×1
symfony ×1
user-profile ×1