休眠 - 双向@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
最满意答案
一个想法:每个可能的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(); }
相关问答
更多-
Dapper是否支持枚举?(Does Dapper support Enums?)[2022-07-17]
枚举支持,但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 exte ...
-
当变量(特别是方法参数)只能从一小组可能的值中取出一个时,应始终使用枚举。 示例将是类型常量(合同状态:“永久”,“临时”,“学徒”)或标志(“执行现在”,“延迟执行”)。 如果使用枚举而不是整数(或String代码),则可以增加编译时检查,避免错误传递无效常量,并记录哪些值合法使用。 BTW,过度使用枚举可能意味着您的方法做得太多(通常最好有几个单独的方法,而不是一种方法,需要几个标志来修改它),但是如果你必须使用标志或类型代码,枚举是要走的路。 作为一个例子,哪个更好? /** Counts numbe ...
-
在SDK Manager中更新您的Google Repository Update your Google Repository in SDK Manager
-
DB2是否支持枚举?(Does DB2 support enums?)[2023-01-11]
没有 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. -
是否可以使用枚举通过注释映射Spring组件?(Is it possible to map Spring components through annotations using enums?)[2023-07-06]
有没有办法通过引用枚举中的值来指定组件的名称? 不,那里没有。 如果注释属性期望enum ,则可以使用enum 。 但是调用方法并不能解析为常量表达式。 您可能认为您可以将该字段设为public并直接访问它 @Named(MY_ENUMS.A.name) 但这不起作用,因为MY_ENUMS.A.name也不是常量表达式。 theat不是常量表达式的实际原因是枚举常量基本上是变量。 有一个常量变量,它是一个常量表达式。 要使变量成为常量变量,它需要是final并使用常量表达式进行初始化。 enum常量是fi ... -
在Springs @Query注释中使用枚举是否有一种优雅的方式?(Is there an elegant way to use enums in Springs @Query annotation?)[2024-01-22]
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 ...
-
枚举支持继承(Enums support for inheritance)[2021-05-03]
不,C ++不允许这样的事情。 Base::Color是与Derived::Color完全独立的类型,与它们没有连接。 这与任何其他嵌套类型没有什么不同; 基类中定义的嵌套类型未连接到派生类中定义的嵌套类型。 枚举也不能从彼此继承。 无论如何,这种事情往往违背良好的OOP做法。 毕竟,如果派生类引入了新的枚举器,基类将如何处理它? 不同的派生类实例将如何处理它? 如果Base定义了对枚举的操作,那么Base定义它所操作的枚举的总和,并且从它派生的每个类应该能够处理所有这些选项。 否则,虚拟接口出现问题。 N ... -
TestNG及其注释(TestNG and its annotations)[2022-02-27]
但是它不会起作用。 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 { ...