How to bypass MapsId for a single default case - mysql

I have created an Employee entity and a corresponding ProfilePicture entity:
#Entity
#Table(name = "employee")
public class Employee{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "first_name", nullable = false)
private String firstName;
#Column(name = "last_name", nullable = false)
private String lastName;
}
#Entity
#Table(name = "profile_picture")
public class ProfilePicture {
#Id
private Long id;
#OneToOne
#MapsId
private Employee employee;
#Column(name = "image")
private byte[] image;
}
Now, I want to add a default image to my profile_picture table. I am using a MySQL database and introducing a default image seems to be a problem, since my profile_picture database entries require an association with an employee. The issue is that MapsId is trying to map the id of the associated employee to the id of my profile-picture. When there is no associated employee, this is impossible.
Does anyone have an idea how to solve this problem? I know I could create a dummy employee, but I don't want to do this. I would like to have an entry in my profile_picture table for which the employee_id column is simply null. Right now, however, this is violating some constraints. I know I will have to think of a different solution, I am just asking for ideas.

Related

JPA mapping for one-to-many collection of shared data, with user specific values

I have a User model that contains a list of achievements
#Table(name = "user")
#Entity
#NamedEntityGraph(name = "User.achievements",
attributeNodes={
#NamedAttributeNode("achievements")
})
#Data
public class User {
#Id
#NotNull
#Column(name = "username")
private String username;
#Column(name = "password")
private String password;
#ElementCollection(fetch = FetchType.LAZY, targetClass = Achievement.class)
private List<Achievement> achievements = new ArrayList<>();
}
Here's the achievement model
#Entity
#Data
#Table(name = "achievement")
public class Achievement {
#Id
#GeneratedValue(generator = "system-uuid")
#GenericGenerator(name = "system-uuid", strategy = "uuid")
private String achievementId;
#Column(name = "title")
private String title;
#Column(name = "description")
private String description;
#Column(name = "achieved", columnDefinition="BOOLEAN DEFAULT false", nullable = false)
private boolean achieved = false;
user_achievements table generated from #ElementCollection mapping, which atm only contains user and achievement foreign keys
I am looking to move the boolean achieved value to the user_achievements table, ideally without having to create a separate model User_Achievements
I am fairly new to using Jpa, but i feel like this scenario is too basic so there must be a straight forward way to do that i cant seem to locate it
#Entity
class UserAchievement {
#EmbeddableId
UserAchievementId id;
#ManyToOne(fetch=LAZY)
#JoinColumn(name="user_username", insertable=false, updatable=false)
User user;
#ManyToOne(fetch=LAZY)
#JoinColumn(name="achivement_achivement_id", insertable=false, updatable=false)
Achivement achivement;
// and other fields
}
class User {
// ...
#OneToMany(mappedBy="user")
List<UserAchievement> userAchievements;
}
and you need to define UserAchievementId

Spring Data JPA (Hibernate) is not creating column as autoincrement

Why is this mapping unable to create the column id as autoincrement?
#Entity(name = "user_role")
public class UserRole {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id", nullable = false, unique = true)
private Long id;
#OneToOne(cascade = CascadeType.MERGE, targetEntity = Role.class)
#JoinColumn(name = "role_id")
private Role role;
#OneToOne(cascade = CascadeType.MERGE, targetEntity = User.class)
#JoinColumn(name = "user_id")
private User user;
}
Thanks in advance!
To use a AUTO_INCREMENT column in your MySQL, you are supposed to use an IDENTITY strategy:
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "id", nullable = false, unique = true)
private Long id;
//Your code
To learn more check this Link
It is clearly mentioned that
The IDENTITY strategy also generates an automatic value during commit for every new entity object.
Well, just to help people.
The problem was the name of my entity "user_role", I don't know why but after changing the entity name to "userrole" everything worked and the id column was created as AUTOINCREMENT.
Hope this helps!

How to read specific fields from mapped object in Hibernate

I've got two entity objects in my database: UserEntity and ItemEntity and they're mapped with OneToMany relationship.
Here is my code:
UserEntity:
#Entity
#Table(name = "users")
public class UserEntity {
#Id
#Column(name = "user_id")
#GeneratedValue
public int user_id;
#Column(name = "userlogin")
public String userlogin;
#Column(name = "userpass")
public String userpass;
#Column(name = "name")
public String name;
#Column(name = "email")
public String email;
....
#JsonBackReference
#OneToMany(mappedBy="user", cascade = { CascadeType.MERGE },fetch=FetchType.EAGER)
private List<ItemEntity> items;
ItemEntity:
#Entity
#Table(name = "items")
public class ItemEntity {
#Id
#Column(name = "id")
#GeneratedValue
private int id;
#Column(name = "title")
public String title;
#Column(name = "info")
public String info;
#JsonManagedReference
#ManyToOne
#JoinColumn(name="user_id")
private UserEntity user;
And now I'm trying to read all my Items from my database with specific fields from users that owns current item. I need only UserEntity name and email.
This code:
Query query = this.sessionFactory.getCurrentSession().createQuery("from ItemEntity WHERE title = :title");
returns all fields from UserEntity also, because it's mapped, but I don't want that, because I'm sending that data as JSON, and someone can see all informations about user who own that item (like user login and password) in some dev tools like Chrome.
How to reach that?
I'd suggest you use DTO.
Covert your entities to DTO and then transform the DTO objects to
json string.
In the DTO populate only those field that you want as part of your response.
This would make your design more clean.
In addition to what's jitsonfire is suggesting, you can write a query like this
select name, email from ItemEntity WHERE title = :title
than get your results like
List<Object[]> result = query.list();
The object array will contain your columns, the list element will equal to rows, so you can do something like
for (Object[] tuple : result) {
tuple[0]; //name
tuple[1]; // email
}

Many to many table with an additional id column

In hibernate if i do the following
class User{
#ManyToMany
private List<UserRole> roles;
}
it creates a separate table User_UserRole where there is two column (user_id and roles_id)
But i want a third column in there e.g "id" which uniquely identify each row and the id's are auto generated.
How do I Do this ???
with default behavior you cannot achieve this, but if you can create the table by yourself in the database (instead of letting hibernate doing it) and then mapping
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "User_UserRole", joinColumns = { #JoinColumn(name = "USER_ID") }, inverseJoinColumns = { #JoinColumn(name = "ROLES_ID") })
private List<UserRole> roles;
of course you will make the id column autoincrement also in the table definition.
You could do that with the following implementation
#Entity
class User{
#Id
private int id;
#OneToMany(mappedBy=role)
private List<UserRole> roles;
}
#Entity
#Table(name="USER_ROLE")
class UserRole{
#Id
private int id; <--ID you were looking for
#ManyToOne
private User user; <--User ID
#ManyToOne
private Role role; <--Role ID
}
#Entity
class Role{
#Id
private int id;
#OneToMany(mappedBy=user)
private List<UserRole> roles;
}
Trick is break ManyToMany relationship in two OneToMany pointing to the Jointable. This works !!
Also you can add another columns in the USER_ROLE table, without breaking anything

How to fetch data from One To One Relationship in SubQuery using Hibernate in spring

I have two table hesk_users, user as under.
#Entity
#Table(name="hesk_users")
public class UserHesk {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column
private int id;
#OneToOne
#JoinColumn(name="user_id")
private User user;
set.... get..
}
#Entity
#Table(name="user")
public class User implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#NotEmpty
#Column
private String firstname;
#OneToOne(mappedBy="user")
private UserHesk userHesk;
set ... get ...
}
I have hibernate query but it not working..
DetachedCriteria detachedCriteria=DetachedCriteria.forClass(UserHesk.class)
.setProjection(Projections.property("user_id"));
Criteria criteria=sessionFactory.getCurrentSession().createCriteria(User.class);
criteria.add(Property.forName("id").notIn(detachedCriteria));
User user=(User)criteria.list().get(0);
System.out.println(user.getFirstname());
System.out.println("Subquery Size "+criteria.list().size());
Error for user_id filed. which can not be get because of relationship.
HQL and criteria queries always use the entity classes and properties. Never the names of the underlying tables and columns. The UserHesk class doesn't have any user_id property. It has a user property.
So the code should be
DetachedCriteria detachedCriteria =
DetachedCriteria.forClass(UserHesk.class)
.setProjection(Projections.property("user.id"));
or even
DetachedCriteria detachedCriteria =
DetachedCriteria.forClass(UserHesk.class)
.createAlias("user", "u")
.setProjection(Projections.property("u.id"));