首页 \ 问答 \ 休眠 - 双向@OneToOne(Hibernate - bidirectional @OneToOne)

休眠 - 双向@OneToOne(Hibernate - bidirectional @OneToOne)

我有2个类:User和UserPicture,它们具有1:1的关系。

public class User {
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="id", nullable = false, unique = true)
 private int id;

     private String firstname;

     private String lastname;

     @OneToOne
     @JoinColumn(name = "picture") //field named "picture" in the database
     private UserPicture userPicture;

     ..
}


public class UserPicture {

     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="id", nullable = false, unique = true)
     private int id;

     private Blob image;

     @OneToOne
     @JoinColumn(name = "user")
     User user;

UserPicture中的'user'将被加载,但user'user'中的'userPicture'不是 - 我做错了什么?

编辑不得不添加,我只是创建一个UserPicture并插入它们(与现有的用户ID) - 也许我需要在UserPicture中级联'用户'?


I have 2 classes: User and UserPicture which have a 1:1 relationship.

public class User {
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="id", nullable = false, unique = true)
 private int id;

     private String firstname;

     private String lastname;

     @OneToOne
     @JoinColumn(name = "picture") //field named "picture" in the database
     private UserPicture userPicture;

     ..
}


public class UserPicture {

     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name="id", nullable = false, unique = true)
     private int id;

     private Blob image;

     @OneToOne
     @JoinColumn(name = "user")
     User user;

'user' in UserPicture will be loaded but 'userPicture' in User not - what did Im wrong?

EDIT Have to add that Im just create a UserPicture and insert them (with existing userId) - maybe I need to cascade 'user' in UserPicture?


原文:https://stackoverflow.com/questions/13044567
更新时间:2023-08-19 19:08

最满意答案

一个想法:每个可能的AdminLevel都是它自己的类。 将该Class传递给注释。

public final class Admin implements AdminLevel { ... }
public final class Anonymous implements AdminLevel { ... }

@Requires(Admin.class) public void protectedMethod() { ... }

@interface Requires {
  Class<? extends AdminLevel> value();
}

One idea: every possible AdminLevel is its own class. Pass the Class of that class to the annotation.

public final class Admin implements AdminLevel { ... }
public final class Anonymous implements AdminLevel { ... }

@Requires(Admin.class) public void protectedMethod() { ... }

@interface Requires {
  Class<? extends AdminLevel> value();
}

相关问答

更多
  • 枚举支持,但Enum不是:p 所以如果你有: class User { public MemberRole Role {get;set;} } 那么应该可以正常工作; 然而 class User { public Enum Role {get;set;} } 将不会。 你可能使用后者? Enums are supported, but Enum isn't :p So if you have: class User { public MemberRole Role {get;se ...
  • 一个想法:每个可能的AdminLevel都是它自己的类。 将该Class传递给注释。 public final class Admin implements AdminLevel { ... } public final class Anonymous implements AdminLevel { ... } @Requires(Admin.class) public void protectedMethod() { ... } @interface Requires { Class
  • 当变量(特别是方法参数)只能从一小组可能的值中取出一个时,应始终使用枚举。 示例将是类型常量(合同状态:“永久”,“临时”,“学徒”)或标志(“执行现在”,“延迟执行”)。 如果使用枚举而不是整数(或String代码),则可以增加编译时检查,避免错误传递无效常量,并记录哪些值合法使用。 BTW,过度使用枚举可能意味着您的方法做得太多(通常最好有几个单独的方法,而不是一种方法,需要几个标志来修改它),但是如果你必须使用标志或类型代码,枚举是要走的路。 作为一个例子,哪个更好? /** Counts numbe ...
  • 没有 DB2不支持ENUMS。 这是一些数据库,我知道哪些suums是MySql和Postgresql,但DB2当然不支持它。 No DB2 does not support ENUMS. The are some database which I am aware of which suports Enums is MySql and Postgresql but DB2 for sure does not support it.
  • 有没有办法通过引用枚举中的值来指定组件的名称? 不,那里没有。 如果注释属性期望enum ,则可以使用enum 。 但是调用方法并不能解析为常量表达式。 您可能认为您可以将该字段设为public并直接访问它 @Named(MY_ENUMS.A.name) 但这不起作用,因为MY_ENUMS.A.name也不是常量表达式。 theat不是常量表达式的实际原因是枚举常量基本上是变量。 有一个常量变量,它是一个常量表达式。 要使变量成为常量变量,它需要是final并使用常量表达式进行初始化。 enum常量是fi ...
  • and where g.status = ?2 应该 and g.status = ?2 and where g.status = ?2 should be and g.status = ?2
  • 那些枚举类看起来很整洁,我常常嫉妒Java的枚举。 我认为通常的规则适用:做最简单的事可能有效。 如果你发现自己在枚举上有多个switch ,那就是气味,是时候考虑你找到的模式了。 否则,为什么要为自己增加一些你不需要的东西呢? Those Enumeration Classes look neat, I've often looked jealously at Java's enums. I think the usual rules apply: do the Simplest Thing That C ...
  • 不,C ++不允许这样的事情。 Base::Color是与Derived::Color完全独立的类型,与它们没有连接。 这与任何其他嵌套类型没有什么不同; 基类中定义的嵌套类型未连接到派生类中定义的嵌套类型。 枚举也不能从彼此继承。 无论如何,这种事情往往违背良好的OOP做法。 毕竟,如果派生类引入了新的枚举器,基类将如何处理它? 不同的派生类实例将如何处理它? 如果Base定义了对枚举的操作,那么Base定义它所操作的枚举的总和,并且从它派生的每个类应该能够处理所有这些选项。 否则,虚拟接口出现问题。 N ...
  • 但是它不会起作用。 TestNG本身使用Java的注释。 您可以在这里查看TestNG的源代码。 它使用像 import java.lang.annotation.Retention; import java.lang.annotation.Target; 这不适用于JVM版本<= 5。 我使用了旧的Java SE开发工具包和此处托管的最旧版本的TestNG。 试过这个: import org.testng.annotations.Test; public class TestClass { ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)