#Entity
#Table(name = "T019_STAFF_PROFILE")
public class UserDetails implements Serializable
{
private static final long serialVersionUID = -723583058586873479L;
#Id
#Column(name = "STAFF_ID", columnDefinition="CHAR(8)")
private String loginId;
#Column(name = "PASSWORD", columnDefinition="CHAR(8)")
private String password;
#Column(name = "FIRST_NAM")
private String firstName;
#Column(name = "LAST_NAM")
private String lastName;
#Column(name = "BUS_OG_CD" , columnDefinition="CHAR(3)")
private String profile;
public UserDetails getLoginDetails(String loginId)
{
UserDetails object =(UserDetails)sessionFactory.getCurrentSession().get(UserDetails.class, loginId);
return object;
}
Check the database to see if the value really exists in the database.
SELECT * FROM T019_STAFF_PROFILE WHERE STAFF_ID = <the value of **loginId**>
For Time being, created a temp user with exactly 8 chars and then tried to access the same user through hibernate. I was able to fetch the record.
#Id
#Column(name = "STAFF_ID", columnDefinition="CHAR(8)")
private String loginId;
This is my column definition. Is there any thing that i need to declare?
Found something interesting. Correct me if the approach is wrong.
Since my db column is declared as CHAR(8), to hibernate, i should always pass 8 char to get the results.
So, if your STAFF_ID is 3 chars, append 5 empty chars and the pass it to hibernate which will fetch your record.
Any thoughts?
Session.get(Class clazz, Serializable id)
Return the persistent instance of the given entity class with the
given identifier, or null if there is no such persistent instance. (If
the instance is already associated with the session, return that
instance. This method never returns an uninitialized instance.)
So check if data exists in your database or not.
Related
I'm currently struggling with trying to change an element value inside my nested List.
UserRepository
List<User> findByUsernameAndIsActiveTrue(String username);
User Entity
public class User {
#NotNull
#Column(name = "id_user")
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
private long id;
#NotNull
#Column(name = "username")
private String username;
#Column(name = "password")
private String password;
#NotNull
#Email
#Column(name = "email")
private String email;
#Column(name = "age")
private int age;
#Column(name = "address")
private String address;
#NotNull
#Column(name = "status")
private Boolean isActive;
UserService
boolean deleteUser(String username);
I'm trying to change the value of the isActive to false so it is counted as a soft delete, but I don't know how the code works in the ServiceImpl. Do I need to stream map the value inside? Since I'm going to use save later, does that mean if the other elements don't have any value inputted it will be null?
Do I need to stream map the value inside?
Simply get the users that you want to delete using UserRepository, then set their active property to false.
Since I'm going to use save later, does that mean if the other
elements don't have any value inputted it will be null?
No need to call save explicitly. Just let the transaction commit and it will figure out what you changed and automatically update these changes to DB when the transaction commit.
After getting the users to be deleted from UserRepository , they are already initialised with the value that are the same as DB record. The properties values will not be null if the related DB column is not null. Just make sure you don't change the properties that you don't intended to update.
The ServiceImpl will look like :
#Service
public class ServiceImpl {
private UserRepository userRepository;
#Transactional
public boolean deleteUser(String username){
List<User> users = userRepository.findByUsernameAndIsActiveTrue(username);
for(User user : users){
user.isActive(false);
}
}
}
Also make sure you enable #Transactional or equivalent for deleteUser().
I have the following problem, which is not easy to explain me. Therefore, I will show you my java code:
Usuarios class:
#Entity
#Table( name="usuarios" )
public class Usuario implements Serializable {
private static final long serialVersionUID = 3L;
#Id
#Column( name="userid", length=8 )
private String userId;
#Column
private String nombre;
#Column
private String apellido;
#Column(name="nro_fun")
private int nroFuncionario;
#OneToMany( cascade=CascadeType.ALL, mappedBy="usuario" )
private List<UsuarioPermisos> permisos;
UsuarioPermisos class:
#Entity
#Table( name="usuarios_permisos" )
public class UsuarioPermisos {
#TableGenerator( name="claveUsuPerm",
table="numerador",
pkColumnName="clave",
valueColumnName="ultimo_numero",
pkColumnValue="ID_USU_PERMISO",
allocationSize=1
)
#Id
#Column
#GeneratedValue(strategy= GenerationType.TABLE, generator="claveUsuPerm")
private int clave;
#Column(name="userid", length=8 )
private String usuario;
#OneToOne
#JoinColumn( name="perId" )
Permisos permiso;
Then, in a servlet, implement the following:
sessionFactory = (SessionFactory) getServletContext().getAttribute( "SESSION_FACTORY" );
session = sessionFactory.openSession();
trn = session.beginTransaction();
Usuario usu = session.get( Usuario.class, varUsrId );
if( usu != null )
session.delete( usu );
The problem is the following:
varUsrId variable is loaded with the value entered by a user from a web form.
if when I run the function get the variable varUsrId has exactly 8 characters (the length of the field in the database table), the delete works well, ie, first remove all objects UsuarioPermisos class, and then removes the Usuario object (from the respective tables in the DB).
But if the variable varUsrId is less than 8 characters (for example, has the value "plopez"), the cascaded delete does not work. That is, does not eliminate UsuarioPermisos objects, however, eliminates the Usuario object. This causes an error in the database due to existing foreign key.
Does anyone have any idea what could be the problem?
These are the versions I'm using:
hibernate 5.0.6,
java 1.8.0_51,
apache-tomcat-8.0.24,
postgres 9.3.9
As you are trying to map the two entities with a OneToMany relationship, try changing the following line:
#Column(name="userid", length=8 )
private String usuario;
to:
#ManyToOne()
#JoinColumn(name="userid")
private Usuario usuario;
How do I create auto delete cascade using ORMLite? I am using ormlite-core and omlite-jdbc version 4.8. I tried
public class Account {
// for QueryBuilder to be able to find the fields
public static final String NAME_FIELD_NAME = "name";
public static final String PASSWORD_FIELD_NAME = "passwd";
#DatabaseField(generatedId = true)
private int id;
#DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false)
private String name;
#DatabaseField(columnName = PASSWORD_FIELD_NAME)
private String password;
}
and another class
#DatabaseTable(tableName = "orders")
public class Order {
public static final String ACCOUNT_ID_FIELD_NAME = "account_id";
#DatabaseField(generatedId = true)
private int id;
#DatabaseField(foreign = true, columnName = ACCOUNT_ID_FIELD_NAME,
canBeNull = false,index = true, foreignAutoRefresh = true,
columnDefinition = "integer references document(id) on delete cascade")
private Account account;
#DatabaseField
private int itemNumber;
#DatabaseField
private int quantity;
#DatabaseField
private float price;
}
But When I delete the parent key record, no exception is being thrown and also, if try to insert records in order table with foreign key values which are not defined in the Account table, no exception is being thrown and the records get created and inserted in database.
But When i delete the parent key record , no exception is being thrown and also , if try to insert records in order table with foreign key values which are not defined in the Account table, no exception is being thrown and the records get created and inserted in database .
Stupid answer but shouldn't the column definition be:
integer references account(id) on delete cascade
I assume that the Account table is named account and not document. Otherwise your SQL looks good.
I would take a look at the schema on the MySQL side to make sure that it matches what you are expecting. I'd also try some inserts from the MySQL command line to see if you can figure out what is going on outside of ORMLite.
Hope this helps.
In my Hibernate Application i'm using create a ValueObject class
#Entity
#Table(name="user")
public class UserVO{
#Id
#Column(name="S_ID")
private String s_id;
#Column(name="FIRSTNAME")
private String firstName;
#Column(name="LASTNAME")
private String lastName;
}
and in my Service class i'm writing like this
public void createOrUpdateUser(UserVO userVO) {
userDAO.createOrUpdateUser(userVO);
}
and in my DAO class i'm writing like this
private EntityManager entityManager;
public void createOrUpdateUser(UserVO userVO) throws DataAccessException {
entityManager.persist(userVO);
}
now i'm calling createOrUpdateUser(userVO) but it give error
Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()
Actually my data base i have created one trigger for user table to generate unique id for s_id column is their any problem for trigger..please suggest me..
If you are using a trigger, the intended generation strategy is org.hibernate.id.SelectGenerator. However, in order to use this strategy, Hibernate must be able to locate the inserted row after insertion to see what value the trigger assigned there are 2 ways to do this.
First is to specifically configure the generator to tell it a column that define a unique key (at least logically) within the table:
#Id
#Column(name="S_ID")
#GeneratedValue( strategy = "trigger" )
#GenericGenerator(
name="trigger", strategy="org.hibernate.id.SelectGenerator",
parameters = {
#Parameter( name="keys", value="userName" )
}
)
private String s_id;
private String userName;
The other is via Hibernate's natural-id support:
#Id
#Column(name="S_ID")
#GeneratedValue( strategy = "trigger" )
#GenericGenerator( name="trigger", strategy="org.hibernate.id.SelectGenerator" ) )
private String s_id;
#NaturalId
private String userName;
GenerationType.IDENTITY may work for you. It will really come down to the JDBC driver and how (if) it implements getGeneratedKeys
ID column must not be null, whatever you do in database driver, will only be trigger before/after insert or any other operation, But according to error it is giving error before any error.
Set something to ID values or try something
#GeneratedValue(strategy = GenerationType.IDENTITY)
as #vinit said :
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#Column(name="S_ID")
private String s_id;
is the best choice ...
I have an embedded PK object that doesn't populate the id field after persisting and flushing to the database. The ID is an auto-increment field in the database.
Now normally, I would just try a refresh, but it throws the following error:
"Entity no longer exists in the database: entity.Customers[ customersPK=entity.CustomersPK[ id=0, classesId=36 ] ]."
public class Customers implements Serializable {
#EmbeddedId
protected CustomersPK customersPK;
...
}
#Embeddable
public class CustomersPK implements Serializable {
#Basic(optional = false)
#Column(name = "id")
private int id;
#Basic(optional = false)
#NotNull
#Column(name = "classes_id")
private int classesId;
.....
}
And here's the code that makes the call
Classes cl = em.find(Classes.class, classId);
CustomersPK custPK = new CustomersPK();
custPK.setClassesId(cl.getId());
Customers cust = new Customers(custPK);
em.persist(cust);
em.flush();
// The problem is right here where id always equals 0
int id = cust.getCustomerspk().getId();
Thanks for the help.
Why would the id not be 0, you have never set it?
If it is a generated id, you need to annotate it using #GeneratedValue, otherwise you need to set the value.