Boolean value of Hibernate data class not updating in the Database - java

In the project I’m working on I have a hibernate data class which uses a DAO to save and update information. I need to set a value to the boolean property 'isPrimary' and save it in the my_class database.
Below is the data class,
#Entity
#Table(name = "my_class")
public class MyClassData {
private String id;
private String name;
private Address address;
// getter, setter
}
class Address {
private String street;
private boolean isPrimary;
// getter, setter
}
when I try to update just the boolean value 'isPrimary' as shown below and try to update the database it is not updated.
MyClassData myClassData = myclassDAO.getById(id);
myClassData.getAddress().setIsPrimary(false);
myclassDAO.update(myClassData); // doesn’t update in the db
However if I try to update boolean value along with another string property both properties are updated in the database
MyClassData myClassData = myclassDAO.getById(id);
myClassData.setName(“John”)
myClassData.getAddress().setIsPrimary(false);
myclassDAO.update(myClassData); // updates both values in the db
Is there a specific reason why this is happening and how can I update just the boolean value in the database?

I do not have a project ready to test your code but I am pretty sure that you need to specify to Hibernate that the class Address is embedded in your entity by adding #Embeddable at the top of the class Address, like this :
#Entity
#Table(name = "my_class")
public class MyClassData {
private String id;
private String name;
private Address address;
// getter, setter
}
#Embeddable
public class Address {
private String street;
private boolean isPrimary;
// getter, setter
}

Related

springboot with MongoDb ,Transient field return null

When i trying to do my RBAC job , I've made a class RolePermission like belows:
#Data
#AllArgsConstructor
#NoArgsConstructor
public class RolePermission extends BaseEntity{
private Long roleId;
private Long permissionId;
#Transient
private List<Permission> permissions;
public RolePermission(Long roleId,Long permissionId){
this.roleId = roleId;
this.permissionId = permissionId;
}
}
class Permission like belows
#AllArgsConstructor
#NoArgsConstructor
#Data
public class Permission extends BaseEntity{
public Permission(Long id, Long parentId, String name){
this.setId(id);
this.parentId = parentId;
this.name = name;
}
private String name;
private Long parentId;
private String url;
private String permission;
private Integer type;
private String icon;
private Integer status;
private Integer ord;
}
Here comes my test :
LookupOperation lookup = Aggregation.lookup("permission", "roleId", "_id", "permissions");
Aggregation aggregation = Aggregation.newAggregation(lookup);
AggregationResults<RolePermission> role_permission = mongoTemplate.aggregate(aggregation, "role_permission", RolePermission.class);
//AggregationResults<Map> role_permission = mongoTemplate.aggregate(aggregation, "role_permission", Map.class);
System.out.println(role_permission.getMappedResults());
//userService.getAllPermissions(u);
When I add #Transient , permissions comes to null
and When I remove #Transient,permissions comes back.
I don't wanna save permissions to MongoDB, so I add #Transient, is there any way i can draw the data back without saving it to the Database. Because I get the permissions data from a relationship collectionRolePermission,not itself.
The #Transient behaves similar to the transient keyword.
transient is used to denote that a field is not to be serialized (and therefore not saved in the database too).
#Transient is used to denote that a field should not persisted in database (but it is still serializable as java object).
Take look this baeldung tutorial for it: https://www.baeldung.com/jpa-transient-ignore-field
There is no way to draw the data back without saving it to the Database because it isn't persisted at all since it is what #Transient is used for.
To get the data back you have to persist it somewhere else, but the best place is in the database. If you don't want to persist it along with the other data, consider saving it in a sperate database. So, you could split user data and authentication/RBAC data.

How to send ArrayList or Sets<Integer> as list filtering in Request Body or URL in PUT Request

I'm trying to write a controller that will function as multiple seat reservation.The Integers list is used for filtering.
My Entity looks like this:
#Entity
#Id
private movieId;
private String movieName;
private String cinemaName;
private String cinemaHall;
private Intger seatingPlace;
private boolean booked;
How Can I pass list or sets in request body to access multiple update seatingPlace. Did I modyfing Enity or connect in smthing relation?
Acutally my multipleUpdate API works using JPA Query findByMovieNameAndCinemaNameAndcinemaHall and return me list wchich
I checking isnt Empty and cheking (field boolean booked) if true so ok u can booked them.
And after that i want filter by passing List seatingPlace and change boolen to false.
Based on my understanding of your requirements, a possible solution could be creating another entity (table) MovieSeatReservation and creating a One to Many relationship from your Entity. It could look like this: (You can replace Entity class name with your real entity name)
#Entity
public class Entity {
#Id
#GeneratedValue
private Long movieId;
private String movieName;
private String cinemaName;
private String cinemaHall;
#OneToMany
private List<MovieSeatReservation> reservedSeatsStatus;
// getters and setters
}
#Entity
public class MovieSeatReservation {
#Id
#GeneratedValue
private Long id;
private boolean isReserved;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "movie_id")
private Entity entity;
// getters and setters
}

ForeignCollection throws SQLException on iterating using streams

I have three entity classes: PlayerData, IpData, and TwinData.
Both TwinData and IpData refer to PlayerData with a foreign key.
Here is the code:
#DatabaseTable
public class PlayerData {
#DatabaseField(generatedId = true)
private int id;
#DatabaseField
private UUID uuid;
#ForeignCollectionField
private ForeignCollection<IpData> ips;
#ForeignCollectionField
private ForeignCollection<TwinData> twins;
// Getters and setters...
}
#DatabaseTable
public class IpData {
#DatabaseField(generatedId = true)
private int id;
#DatabaseField(foreign = true)
private PlayerData playerData;
#DatabaseField
private Date firstUsage;
#DatabaseField
private String address;
// Getters and setters...
}
#DatabaseTable
public class TwinData {
#DatabaseField(generatedId = true)
private int id;
#DatabaseField(foreign = true)
private PlayerData mainPlayer;
#DatabaseField
private boolean adminNoticed;
#DatabaseField
private UUID uuid;
// Getters and setters...
}
When I try to iterate over PlayerData.ips with streams, I get SQLException [SQLITE_ERROR] SQL error or missing database (no such column: playerData_id). Tables are auto-generated with TableUtils.createIfNotExists().
This is the code that throws that exception:
// playerData is retrieved from Dao<PlayerData, Integer>
playerData.getIps().stream().noneMatch(ip -> ip.getAddress().equals(playerIp))
I just don't really get why it attempts to select playerData_id column.
Should I manually create database tables for TwinData and IpData with playerData_id column? Haven't tried this yet, but I don't think it would help.
When I try to iterate over PlayerData.ips with streams, I get SQLException[SQLITE_ERROR] SQL error or missing database (no such column: playerData_id). Tables are auto-generated with TableUtils.createIfNotExists().
You will get that error if playerData_id is not a field in the table. I'm surprised that TableUtils.createIfNotExists() did not create a field playerData_id in the IpData table. I suspect that the tables existed already and you are not using ORMLite generated schema.
By default when ORMLite sees a foreign = true field, it will store the id of that type in the entity in question. See the foreign fields documentation. If your schema is defining the relationship between the entities differently, for example with another table to define the relationship, then you will need to setup that entity by yourself and query it using the DAO for that entity.
You maybe should look at the foreign object example for more information.
Hope this helps.

Nested reference object is persisting as null

I am trying to store below pojo in couchbase with spring-data, but persist json is storing "user field of type User" as null.
#JsonInclude(value = Include.NON_EMPTY)
#Document
public class ProjectXYZ {
#Id
#GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
#Field
private String prjname;
#Field
private User user;
//setter and getter
}
Update:
User Pojo
#JsonInclude(value = Include.NON_EMPTY)
#Document
public class User {
#Id
#Field
private String id;
#Field
private String email;
#Field
private String firstName;
#Field
private String lastName;
//setter and getter
}
And as below I am saving it, All works fine and as expected but User object get stored as null.
ProjectXYZ project = new ProjectXYZ();
project.setUser(getUser(request));
project = projectXYZService.createProject(project);
References are not directly supported through Spring data couchbase as it needs to store meta information about the reference document id.
However there is support for fetching associated entities through N1QL ANSI Joins available in current Lovelace (3.1.x) versions. Here is the documentation for it.

Hibernate: Add a property in my class that is not mapped to a db-table

I have a table tbl_sky that has 2 properties name and model and I would use Hibernate annotation like;
#Entity
#Table(name="tbl_sky")
public class Sky implements Serializable {
private String name;
private String model;
private String status;
#Id
public String getName() {
return name;
}
.
.
.
But I need to add one more property status that does not exist in the table but is needed in the class. How could I declare that property so that I have it in my class but not in my db-table?
All help is appreciated.
Use #Transient annotation for field you are not going to store in DB:
#Transient
public String getStatus() {
return status;
}
or:
#Transient
private String status;
Mark it as #Transient, and it won't be part of the DB schema.
If you annotate a field with #Transient it will not be persisted.

Categories

Resources