我正在使用带注释的Hibernate(在spring中),并且我有一个对象,它有一个有序的,多对一的关系,一个子对象有一个复合主键,其中一个组件是一个外键回到父对象的id.
结构看起来像这样:
+=============+ +================+
| ParentObj | | ObjectChild |
+-------------+ 1 0..* +----------------+
| id (pk) |-----------------| parentId |
| ... | | name |
+=============+ | pos |
| ... |
+================+
Run Code Online (Sandbox Code Playgroud)
我尝试了各种注释组合,但似乎都没有.这是我能够提出的最接近的:
@Entity
public class ParentObject {
@Column(nullable=false, updatable=false)
@Id @GeneratedValue(generator="...")
private String id;
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER, cascade={CascadeType.ALL})
@IndexColumn(name = "pos", base=0)
private List<ObjectChild> attrs;
...
}
@Entity
public class ChildObject {
@Embeddable
public static class Pk implements Serializable {
@Column(nullable=false, updatable=false)
private String parentId;
@Column(nullable=false, updatable=false)
private …
Run Code Online (Sandbox Code Playgroud) 我正在使用使用JDBI的dropwizard创建一个简单的REST应用程序.下一步是集成一个与另一个具有一对多关系的新资源.到目前为止,我无法弄清楚如何在我的DAO中创建一个方法来检索一个包含另一个表中对象列表的对象.
POJO表示将是这样的:
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Account {
private int id;
private String name;
private List<User> users;
public Account(int id, String name, List<User> users) {
this.id …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用@OrderColumn
Hibernate 3.5 的注释
@OneToMany(mappedBy = "parent",fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@OrderColumn(name = "pos")
private List<Children> childrenCollection;
Run Code Online (Sandbox Code Playgroud)
检索数据时,一切正常.但我不能让它重新排序列表中的元素并将新订单保存到数据库.
我的表:
产品:id,名称
优惠:id,value,product_id
实体:
@Entity
@Table(name="product")
public class Product implements Serializable {
@OneToMany(mappedBy="product")
private Set<Offer> offers;
...
}
@Entity
@Table(name="offer")
public class Offer implements Serializable {
@ManyToOne
@JoinColumn(name="PRODUCT_ID")
private Product product;
...
}
Run Code Online (Sandbox Code Playgroud)
当我尝试从表中获取一些数据时Product
,我得到一个java.lang.NullPointerException
,并且这段代码:product.getOffers()
返回:
{IndirectSet:未实例化}
如何解决这个问题?
我正在构建的应用程序中的一个要求是表单输入,它为单个字段接收不同数量的项目.例如,我参加的运动是('足球','网球','门球').
可以玩(可以说)有限数量的运动,因此应该从表格输入中的"下拉"类型列表中选择这些项目.
该表格的下游将是两个具有一对多关系的表格.因此,从上面看,"user"表将有一行,而"user_sports"表将有三行.然后,这些将由用户表中的id字段链接.
我无法找到在文档中可以实现的功能(也许我不是在寻找正确的东西).下面是我找到的最接近的,但仅用于从下拉列表中选择单个项目.
http://laravel.com/docs/html#drop-down-lists
是否有一个解决方法可以让我使用Laravel框架启动并运行此表单元素?
或者,是否有其他方法可以实现这种功能,而不会损害用户体验?
如果我有多对多的关系,那么用它的sync
方法更新关系是非常容易的.
但是我会用什么来同步一对多的关系呢?
posts
:id, name
links
:id, name, post_id
在这里,每个Post
可以有多个Link
s.
我想将与数据库中特定帖子相关联的链接与输入的链接集合(例如,从我可以添加,删除和修改链接的CRUD表单)同步.
应删除数据库中不存在于我的输入集合中的链接.应更新数据库和输入中存在的链接以反映输入,并且仅在我的输入中存在的链接应作为新记录添加到数据库中.
总结所需的行为:
我在我的网络应用程序中使用JPA而不是Hibernate.这是两个实体(仅显示getter):
class Child {
private Parent parent;
@ManyToOne(optional=false)
@JoinColumn(name="parent_id", referencedColumnName="parent_id", nullable=false, updatable=false)
public Parent getParent() {
return parent;
}
}
class Parent {
private Collection<Child> children;
@OneToMany(fetch=FetchType.EAGER, mappedBy="parent", cascade={CascadeType.ALL})
public Collection<Child> getChildren() {
return children;
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,Parent
并将其Child
视为"一对多".
现在我需要加载一个Parent
实例,删除一些或所有孩子并保存更改.以下代码对我不起作用:
Parent p = entityManager.find(Parent.class, 12345L); // load entity
p.getChildren().clear(); // remove all children
entityManager.merge(p); // try to save
Run Code Online (Sandbox Code Playgroud)
在上面的示例中未删除子实体.现在我必须手动呼叫entityManager.remove()
每个孩子.
有没有更简单的方法来管理儿童收藏?
请注意,我不想使用特定于Hibernate的功能,只需使用纯JPA.
我有一个关于Hibernate 3.6.7和JPA 2.0的问题.
考虑以下实体(为简洁起见,省略了一些getter和setter):
@Entity
public class Parent {
@Id
@GeneratedValue
private int id;
@OneToMany(mappedBy="parent")
private List<Child> children = new LinkedList<Child>();
@Override
public boolean equals(Object obj) {
return id == ((Parent)obj).id;
}
@Override
public int hashCode() {
return id;
}
}
@Entity
public class Child {
@Id
@GeneratedValue
private int id;
@ManyToOne
private Parent parent;
public void setParent(Parent parent) {
this.parent = parent;
}
@Override
public boolean equals(Object obj) {
return id == ((Child)obj).id;
}
@Override
public int hashCode() { …
Run Code Online (Sandbox Code Playgroud) 在Parent类中有一个列表List.保存父级时,已添加或更改的子级应由休眠保存/更新.
我已经找到了很多这方面的解释,但是,我只是没有得到它的工作.
Parent.class尝试A.
@Entity
public class Parent {
// id and other attributes
@OneToMany(mappedBy = "parent")
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.ALL)
protected List<child> children;
Run Code Online (Sandbox Code Playgroud)
Parent.class尝试B
@Entity
public class Parent {
// id and other attributes
@OneToMany(mappedBy = "parent", cascade = { javax.persistence.CascadeType.ALL },
orphanRemoval = true)
@org.hibernate.annotations.Cascade({
org.hibernate.annotations.CascadeType.PERSIST,
org.hibernate.annotations.CascadeType.MERGE,
org.hibernate.annotations.CascadeType.REFRESH,
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.REPLICATE,
org.hibernate.annotations.CascadeType.LOCK,
org.hibernate.annotations.CascadeType.DETACH })
protected List<child> children;
Run Code Online (Sandbox Code Playgroud)
将子项添加到新父项.之后两者都被保存了
sessionFactory.getCurrentSession().saveOrUpdate(parent);
Run Code Online (Sandbox Code Playgroud)
但是,当刷新时,我收到以下错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: de.pockettaxi.backend.model.ride.RideSingle
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275) …
Run Code Online (Sandbox Code Playgroud) 我是核心数据建模的新手,我很难理解一对多关系是如何工作的.
我有一个名为的父实体Task
,它可以有几个Comment
实体实例.我这样建模:on Comments
,与实体一起Task
调用目的地的关系.在,关系称为,其目的地,两个关系彼此相反. task
Task
Task
comments
Comment
未定义反向导致警告或错误消息.虽然这种方式的建模有效,但我注意到,一旦我为给定的第二个注释创建Task
,第一个被替换(一对一的关系).
告诉核心数据模型这种关系允许多个注释合在一起的正确方法是什么Task
?
此外,由于CoreData似乎自己管理主键,我如何创建一个NSPredicate
检索给定任务的所有注释?
谢谢你的任何建议!
one-to-many ×10
hibernate ×5
java ×4
jpa ×3
jpa-2.0 ×2
laravel ×2
php ×2
annotations ×1
cascade ×1
core-data ×1
crud ×1
dropwizard ×1
eloquent ×1
entity ×1
forms ×1
ios ×1
jdbi ×1
join ×1
objective-c ×1
orm ×1