0

I want to update this table in the Oracle database with SQL Developer

Users Table

This is my java entity

@Entity 
@Table(name = "Users")
public class User {

    private int idUser;
    
    private String login;
    
    private String password;
    
    private String actived;
    
    
    public User() { }
    
    public User( String login, String password ) {
        super();
        this.setLogin( login );
        this.setPassword( password );
    }

    @Id
    @Column(name = "USER_ID", unique = true, nullable = false, scale = 0)
    public int getIdUser() {
        return idUser;
    }
    
    @Column(name = "USER_LOGIN", nullable = false)
    public String getLogin() {
        return login;
    }
    
    public void setLogin(String login) {
        this.login = login;
    }

    @Column(name = "USER_PASSWORD", nullable = false)
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    @Column(name = "ACTIVED", length=1)
    public String getActived() {
        return actived;
    }
    
    public void setActived(String actived) {
        this.actived = actived;
    }
    
    
    
    
    public String toString() {
        return this.idUser + ": " + this.login + "/" + this.password 
             ;
    }
    
}

And my service

@Service
@Transactional
public class MyServiceImpl implements MyService {
    private static final long serialVersionUID = 8393594103219622298L;

    public void activedMyUser(int idUser, boolean isActived) {
    String query1 = "update USERS";
    if(isActived) {
        query1 += " set ACTIVED='T' " ;
    } else {
        query1 += " set ACTIVED='F' " ;
    }
    query1 += " where USER_ID=? " ;
    
    SQLQuery query = sessionFactory.getCurrentSession().
            createSQLQuery(query1);
    query.setParameter(0, idUser);
    query.executeUpdate();
}
    
}

And when i call activedMyUser(3, true), In my USERS table the row with id equals 3 is not updated in oracle database with sql developper. I must do a commit after my request, how can i do it programmatically in java. I have no error in my sql native request. How xcan i solve this problem ?

wolφi
  • 8,091
  • 2
  • 35
  • 64
lnikoli31
  • 11
  • 4
  • 2
    Are you committing the change? Until a COMMIT is performed only the session which performs the update can see the change - no one else can. – Bob Jarvis - Слава Україні Apr 11 '21 at 13:02
  • hi @BobJarvis-ReinstateMonica , how can i do commit after query.executeUpdate(). Can you show me an example with java code – lnikoli31 Apr 11 '21 at 18:52
  • See https://stackoverflow.com/questions/14581865/hibernate-flush-and-commit – Bob Jarvis - Слава Україні Apr 11 '21 at 19:48
  • Hi @BobJarvis-ReinstateMonica , i must do this : String query1 = "update USERS"; if(isActived) { query1 += " set ACTIVED='T' " ; } else { query1 += " set ACTIVED='F' " ; } query1 += " where USER_ID=? " ; SQLQuery query = sessionFactory.getCurrentSession(). createSQLQuery(query1); query.setParameter(0, idUser); query.executeUpdate();Transaction tx = session.beginTransaction();tx.commit(); can you confirm me please or i must do otherwise – lnikoli31 Apr 11 '21 at 20:55
  • I'm not a Java or Hibernate guy, but it looks reasonable to me. – Bob Jarvis - Слава Україні Apr 11 '21 at 21:18
  • hi @BobJarvis-ReinstateMonica , do Transaction tx = session.beginTransaction();tx.commit(); i have my row is updated in database but i got an error Transaction not successfully started in my eclipse – lnikoli31 Apr 12 '21 at 05:59

0 Answers0