gpt4 book ai didi

java - 如何更新数据库行

转载 作者:行者123 更新时间:2023-12-02 12:03:14 25 4
gpt4 key购买 nike

基本上,我想检查用户是否存在,我想使用新对象设置进行更新。我被更新条款困住了。我该怎么做??

我想要更新设置中的所有数据而不仅仅是用户字符串。

   Settings settings = new Settings();
settings.setBaselineImg(baselineImg);
settings.setGitlabUrl(gitlabUrl);
settings.setGitlabToken(gitlabToken);
settings.setProfileProject(profileProject);
settings.setWebCSRProject(webCSRProject);
settings.setWebAdminProject(webAdminProject);
settings.setWebClientProject(webClientProject);
settings.setDockerHostname(dockerHostname);
settings.setDockerIP(dockerIP);
settings.setDockerPort(dockerPort);
settings.setUser(user);

//if settings for this user already exist just Update them
CheckIfSettingsForUserExist(user,settings);
//else Add to database
ManageDatabase.AddToDatabase(settings);

}

private static void CheckIfSettingsForUserExist(String user, Settings settings) {
SessionFactory factory = HibernateUtil.GetSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
try {
String hql = "FROM Settings s WHERE s.user = :user";
Query query = session.createQuery(hql);
query.setParameter("user",user);
//If settings for user already exist
if(query.list().size() > 0)
{
//Update
String hqlUpdate = "UPDATE FROM Settings";
Query queryUpdate = session.createQuery(hqlUpdate);
queryUpdate.setParameter("user", user);

}
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}

最佳答案

Query query = session.createQuery("update settings set user =  :user");
query.setParameter("user",user);
int result = query.executeUpdate();

我希望查询中会有一个 where 子句,因为此更新会将所有行更新为新用户(字符串值)。

关于java - 如何更新数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47094062/

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