gpt4 book ai didi

Java集合: Optimal way to update local database via a periodic job

转载 作者:行者123 更新时间:2023-12-01 13:47:30 24 4
gpt4 key购买 nike

我有一个基于 java 的应用程序,它定期从 Web 服务获取数据并将数据保存到本地数据库。本地数据库中的数据永远不会被删除。当在一段时间内,特定记录没有从Web服务返回时,则本地数据库表中该记录的标志之一被改变以指示它没有从Web服务返回。因此,要求是使用从 Web 服务接收到的更改来更新本地数据库,并更新未返回记录的标志。

我编写了以下代码来更新对象级别的状态(在持久化到数据库之前),但我认为这不是最佳的。请告知如何优化此流程?

import java.util.ArrayList;
import java.util.List;

public class ListUpdateTest
{
public static void main(String[] args)
{
List<Person> local = new ArrayList<Person>(); // records in the local database

local.add(new Person("firstName1", "lastName1", "email1", true));
local.add(new Person("firstName2", "lastName2", "email2", true));
local.add(new Person("firstName3", "lastName3", "email3", true));

List<Person> remote = new ArrayList<Person>();// returned from the webservice

remote.add(new Person("firstName1", "lastName1", "email1", true));

update(local, remote);

}

private static void update(List<Person> local, List<Person> remote)
{
for (Person localPerson : local)
{
if (!remote.contains(localPerson))
{
localPerson.setActiveFlag(false);
// save the updated record to the database
}
}

for (Person remotePerson : remote)
{
if (!local.contains(remotePerson))
{
// persist to database
}
else
{
// update local record
}
}
}
}


class Person
{
int id;
String firstName;
String lastName;
String email;
boolean activeFlag;

public Person(String firstName, String lastName, String email, boolean activeFlag)
{
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.activeFlag = activeFlag;
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getEmail()
{
return email;
}

public void setEmail(String email)
{
this.email = email;
}

public boolean isActiveFlag()
{
return activeFlag;
}

public void setActiveFlag(boolean activeFlag)
{
this.activeFlag = activeFlag;
}

@Override
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
if (obj == null
|| obj.getClass() != this.getClass())
{
return false;
}

Person guest = (Person) obj;
return id == guest.id
&& (firstName == guest.firstName || (firstName != null && firstName.equals(guest
.getFirstName())))
&& (lastName == guest.lastName || (lastName != null && lastName.equals(guest
.getLastName())));
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime
* result + ((firstName == null)
? 0 : firstName.hashCode());
result = prime
* result + id;
result = prime
* result + ((lastName == null)
? 0 : lastName.hashCode());
return result;
}

@Override
public String toString()
{
return "Person [id="
+ id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
+ ", activeFlag=" + activeFlag + "]";
}
}

为了完成这些数据库操作,我使用 Spring data jpa。

最佳答案

在执行更新之前,您可以将数据库中每个人员记录的 boolean 值设置为 false。然后所有更新的人员记录将被设置回 true。

关于Java集合: Optimal way to update local database via a periodic job,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20253024/

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