gpt4 book ai didi

java - Hibernate merge 同时进行插入和删除

转载 作者:行者123 更新时间:2023-11-30 22:43:33 27 4
gpt4 key购买 nike

我真的是 Hibernate 的新手。这是我的问题:

我有一个中央数据库,可以重新组合来自许多代理的数据。

每个代理必须只将与他有关的数据存储到他自己的数据库中。

我需要在服务器/代理之间同步数据

我有两个案例:

1)第一次同步(agent上的DB为空)

2)完全同步(删除/添加新元素/更改等...)

我有一个盒子实体和一个用户实体,它们之间的关系是多对多

盒子实体(部分)是这样描述的:

 @Entity
//@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name="box")
@NamedQuery(name="Box.findAll", query="SELECT b FROM Box b")
@JsonIdentityInfo(generator= ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Box extends BasicData implements Serializable {
....

//bi-directional many-to-many association to User
@ManyToMany(mappedBy="boxs")
private Set<User> users;

//bi-directional many-to-one association to BoxUser
@OneToMany(mappedBy="box", orphanRemoval=true)
@Cascade({ org.hibernate.annotations.CascadeType.ALL})
private Set<BoxUser> boxUsers;

getters /setters
....

BoxUser 实体的描述(部分)如下:

public class BoxUser implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private BoxUserPK id;

//bi-directional many-to-one association to Box
@ManyToOne
@JoinColumn(name="boxid", nullable=false, insertable=false, updatable=false)
@Cascade({ org.hibernate.annotations.CascadeType.ALL})
private Box box;

//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="userid", nullable=false, insertable=false, updatable=false)
@Cascade({ org.hibernate.annotations.CascadeType.ALL})
private User user;

getters /setters
....

用户实体(部分)是这样描述的:

public class User extends BasicData implements Serializable {
private static final long serialVersionUID = 1L;

//bi-directional many-to-one association to BoxUser
@OneToMany(mappedBy="user")
private Set<BoxUser> boxUsers;

//bi-directional many-to-many association to Box
@ManyToMany
@JoinTable(
name="box_user"
, joinColumns={
@JoinColumn(name="userid", nullable=false)
}
, inverseJoinColumns={
@JoinColumn(name="boxid", nullable=false)
}
)
private Set<Box> boxs;

getters /setters
....

当我尝试保存/更新(我正在使用合并方法)代理端的数据时,我看到数据已插入但立即被删除。

我不明白为什么。这是 hibernate 的输出:

Hibernate: 
insert
into
box_user
(role, boxid, userid)
values
(?, ?, ?)
2015-05-20 15:14:23.818 TRACE: org.hibernate.type.EnumType - Binding [ADMIN_BOX] to parameter: [1]
2015-05-20 15:14:23.819 TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [1]
2015-05-20 15:14:23.819 TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [4]
Hibernate:
delete
from
box_user
where
userid=?
2015-05-20 15:14:23.831 TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [4]

这里是进行同步的方法:

private void synchroUser(Box internalBox, Long boxId)
{

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
BoxUser[] remoteBUList = restTemplate.getForObject(DOMITIK_URL + RestURIConstant.DOMITIK_WS + "getboxuserbyboxid/{boxid}" ,BoxUser[].class,boxId);
if(internalBox == null)
{
boolean firstAdd = true;
for (BoxUser curBoxUser : remoteBUList)
{
// this avoid to save more than one time the box (exception occurred if trying to save several time.
if(!firstAdd)
curBoxUser.setBox(null);

boxUserService.mergeElement(curBoxUser);
firstAdd = false;
}
}
else
{
List<BoxUser> localBUList = boxUserService.findAll();

//deleting users that were removed from server
for (BoxUser localBoxUser : localBUList)
{
boolean userFound = false;
for (BoxUser remoteBoxUser : remoteBUList)
{
if(localBoxUser.getUser().getId() == remoteBoxUser.getUser().getId())
{
userFound = true;
break;
}
}

if(!userFound)
userService.deleteElement(localBoxUser.getUser());
}

//adding new users
for (BoxUser curBoxUser : remoteBUList)
{
boolean userFound = false;
for (BoxUser localBoxUser : internalBox.getBoxUsers())
{
if(localBoxUser.getUser().getId() == curBoxUser.getUser().getId())
{
userFound = true;
break;
}
}

if(!userFound)
{
curBoxUser.setBox(null);
boxUserService.mergeElement(curBoxUser);
}
}

}
}

我真的卡住了,需要明白我做错了什么......

感谢您的时间和帮助

最佳答案

我猜在你的配置中你有属性hbm2ddl.auto,其值为“create-drop”,它会在 session 结束后自动删除数据并删除模式

关于java - Hibernate merge 同时进行插入和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30349858/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com