Container managed security in Glassfish - java

I have followed this blog-tutorial and successfully got it to work:
http://jugojava.blogspot.com/2011/02/jdbc-security-realm-with-glassfish-and.html
I have named my two entities Group and User. The have a bi-directional many-to-many relationship.
Now the reason I have done it as in the blog is because I am making an administrator page where I want to be able to add new users. I also let users have the oppertunity to register them self, and they will have the role user.
#Entity
public class Group implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String groupName;
#ManyToMany
#JoinTable(joinColumns = {#JoinColumn(name="group_id")}, inverseJoinColumns = {#JoinColumn(name="user_id")})
private List<User> users;
....
}
and
#Entity
#Table(name = "app_user")
public class User implements Serializable {
public static final String ALL = "User.all";
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
private Integer age;
private String username;
private String email;
private String password;
#ManyToMany(mappedBy = "users")
private List<Group> groups;
....
}
My question is how I would assign the group user to a user when it register without me picking the groups from a list in the view?
This is what I have done in the application code but it binds the code the the id of the group in the database, are there better ways?
Method from EJB
public void persistAsUser(User user) {
Group group = new Group(2L, "user");
user.addGroup(group);
userRepository.persist(user);
}

You may want to define a UNIQUE index on the field groupName. Then, create a Data Access Object for the Group table, which provides a method for getting a Group from a groupName (code not tested):
public class GroupDAO implements Serializable {
#PersistenceContext private EntityManager em;
public Group findByGroupName(String groupName) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Group> cq = cb.createQuery(Group.class);
Root<Group> group = cq.from(Group.class);
cq.where(cb.equal(group.get(Group_.groupName), groupName));
TypedQuery<Group> q = em.createQuery(cq);
return q.getSingleResult();
}
}
If you don't like Criteria Builder, you can use a Named Query. Add this annotation to your Group Entity Class:
#NamedQuery(name = "Group.findByGroupname", query = "SELECT f FROM group f WHERE f.groupname = :groupname")
and build a Named Query as follows:
return em.createNamedQuery("Group.findByGroupname").setParameter("groupname", groupName).getResultList();

Related

Java entity modeling with table that relates to several others

I have a doubt about how the modeling of my entity would be. Come on, I have a table in the database that serves to save documents from my system, this table has the columns id, fk_id (element foreign key), fk_table (entity name) and file_name (stores the name of my file) .
I did a lot of research before posting my question here, but I didn't find anything related to it, what would my entities, user, patient and doctor?
DB:
id
fk_id
fk_table
file_name
1
21
user
test1.jpg
2
32
doctor
test2.pdf
3
61
user
test10.pdf
4
100
patient
test5.jpg
Class:
public class User{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String LastName;
// What would a one-to-many relationship look like?
}
public class patient{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
// What would a one-to-many relationship look like?
}
You can use #Where. But be aware that #Where is a Hibernate annotation. It's not in the JPA standard.
For example in the User entity: (I assume that your table is mapped to an entity called Document)
#Where( clause = "fk_table = 'user'")
#JoinColumn(name = "fk_id")
#OneToMany
private List<Document> documents = new ArrayList<>( );
The following is based only on standard JPA annotations. The idea is to create an inheritance hierarchy for the documents table. The base is:
#Entity
#Table(name = "XX_DOCUMENT")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "fk_table")
public abstract class BaseDocument {
#Id
#GeneratedValue(strategy=SEQUENCE)
private Long id;
#Column(name = "file_name")
private String fileName;
}
Here we define that all entities extending this will go to the same table, with the fk_table column to discriminate. The entities extending it are defined as follows:
#Entity
#DiscriminatorValue("doctor")
public class DoctorDocument extends BaseDocument {
#ManyToOne
#JoinColumn(name = "fk_id")
private Doctor doctor;
}
#Entity
#DiscriminatorValue("patient")
public class PatientDocument extends BaseDocument {
#ManyToOne
#JoinColumn(name = "fk_id")
private Patient patient;
}
// and so on
The interesting thing is that we are reusing the column fk_id to point to the right table. From a small experiment, Hibernate seems to not have problems with it. I would suggest that you manage the DB creation another way just to be safe.
The Doctor, Patient etc need not have a common base class, e.g.:
#Entity
#Table(name = "XX_DOCTOR")
public class Doctor {
#Id
#GeneratedValue(strategy=SEQUENCE)
private Long id;
#OneToMany(mappedBy = "doctor")
private Collection<DoctorDocument> documents = new ArrayList<>();
// any doctor-specific fields
}
#Entity
#Table(name = "XX_PATIENT")
public class Patient {
#Id
#GeneratedValue(strategy=SEQUENCE)
private Long id;
#OneToMany(mappedBy = "patient")
private Collection<PatientDocument> documents = new ArrayList<>();
// any patient-specific fields
}
// and so on
You can read a (doctor, patient, ...)'s documents from the relevant collection. You can even query BaseDocument instances based on any criteria.
You can even go ahead and do more fabcy stuff with the Java code. E.g. define an interface HasDocuments:
public interface HasDocuments<D extends BaseDocument> {
Collection<D> getDocuments();
}
Doctor, Patient, ..., implements this, so they can all be treated the same way.

How to get Entity based on member object's field value rather than member objects ID

I have three classes and code for the same is displayed below
Enquiry Class:
#Entity
public class Enquiry
{
#Id
#GeneratedValue
private int id;
private String name;
private String discription;
private int status;
#Temporal(TemporalType.DATE)
private Date enquiryDate;
}
User Class:
#Entity
public class User
{
#Id
#GeneratedValue
private int id;
private String name;
private String userId;
private String password;
}
UserEnquiryUserEnquiryMapping Class:
#Entity
public class UserEnquiryMapping
{
#Id
#GeneratedValue
private int id;
#ManyToOne
private User user;
#ManyToOne
private Enquiry enquiry;
}
Now suppose if we want to get Enquiry(s) for a particular User than we can easily get it by passing a User object and hibernate will generate query by using id field from User object, and code for the same scenario is mentioned below.
EntityManager entityManager = session.getEntityManagerFactory().createEntityManager();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserEnquiryMapping> criteria = builder.createQuery(UserEnquiryMapping.class);
Root<UserEnquiryMapping> root = criteria.from(UserEnquiryMapping.class);
criteria.select(root);
criteria.where(builder.equal(root.get("user"), user));
userEnquiries = entityManager.createQuery(criteria).getResultList();
But my requirement is I want to get User enquiries on the basis of user's name or we can say that I want to generate query like this
Select * from UserEnquiryMapping inner join Enquiry on UserEnquiryMapping.Enquiry_ID = Enquiry.ID inner join User on UserEnquiryMapping.User_ID = User.ID where User.name="Test";
How can I do this?
builder.equal(root.get("user").get("name"),user.getName());
glad it help you!

Spring Data Repository with Inheritance.JOINED

I have designed the entities in my application to follow the Hibernate's inheritance strategy Inheritance.JOINED.
The base abstract class is UserTable and the concrete derived classes are ClientTable and the OwnerTable.
The thing that I want to achieve is to have a single repository in which I could find any UserTable (both ClientTable and OwnerTable) by the Id or Email. The important requirement is that they can be cast to the correct type once they are fetched, and that they carry their specific fields (and not just the ones inherited from UserTable).
Equally important, I should be able to persist those entities through the same repository, ie. repository.save(clientTable).
So something like this should be possible:
UserTableRepository repository = // ...
int clientId = 3;
int ownerId = 5;
ClientTable clientTable = (ClientTable) repository.findByUserId(clientId);
OwnerTable clientTable = (OwnerTable) repository.findByUserId(ownerId);
clientTable = (ClientTable) repository.findByEmail(clientEmail);
ownerTable = (OwnerTable) repository.findByEmail(ownerEmail);
My entities look like this.
UserTable.java
#Entity
#Table(name = "USER")
#Inheritance(strategy = InheritanceType.JOINED)
public abstract class UserTable implements Serializable {
#Column(name = "USER_ID", nullable = false)
private Long userId;
#EmbeddedId
private UserTablePK userTablePK;
#Column(name = "PASSWORD", nullable = false, length = 512)
private String password;
#Column(name = "FIRSTNAME", nullable = false, length = 256)
private String firstName;
// get, set
}
.. and a primary key
UserTablePK.java
#Embeddable
public class UserTablePK implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name = "EMAIL", nullable = false, length = 256)
private String email;
public UserTablePK() {
}
public UserTablePK(String email) {
this.email = email;
}
// get, set
}
ClientTable.java
#Entity
#Table(name = "CLIENT")
public class ClientTable extends UserTable implements Serializable {
#Column(name = "SHOPING_POWER")
private Integer shoppingPower;
#Column(name = "SHOPPING_FREQUENCY")
private Integer shoppingFreq;
// get, set
}
OwnerTable.java
#Entity
#Table(name = "OWNER")
public class OwnerTable extends UserTable implements Serializable {
#Column(name = "EFFICIENCY")
private String efficiency;
#Column(name = "SALARAY")
private Integer;
// get, set
}
Now once the entities are set, I need to write a repository that would act like described above, and here is where I need some help. I was thinking to write something like this.
UserTableRepository.java
#Repository
public interface UserTableRepository extends CrudRepository<UserTable, UserTablePK> {
#Query("SELECT e FROM UserTable e WHERE e.userTablePK.email = ?1")
public UserTable findByEmail(String email); // if owner email, retrieve OwnerTable; if client email, retrieve ClientTable instance
#Query("SELECT e FROM UserTable e WHERE e.userId = ?1")
public UserTable findByUserId(Long userId); // if owner id, retrieve OwnerTable; if client id, retrieve ClientTable instance
}
I didn't actually try this yet, because I want to check if this is even the right route to take, but: I am rather skeptical that this Query would work. Because in order to retrieve the whole subclass object, some kind of JOIN should be used, and the problem is that I can't explicitly say ie. JOIN ClientTable because then I would not be able to fetch any OwnerTable entities.
How can I achieve that I can fetch both subclasses from the same repository?
UPDATE
Also, the requirement is to be able to fetch for the specific subclass. So something like:
List<ClientTable> clients = repository.findAllClients();
Is that possible using the same repository, or should I write repositories specific for subclasses? For example.
#Repository
public interface ClientTableRepository extends CrudRepository<ClientTable, ClientTablePK> {
// empty
}
... and then call them like:
ClientTableRepository repository = // ...
List<ClientTable> clients = repository.findAll();
Would that be enough for the JPA to determine how to find all the instances of just a specific subclass?
What you want to achieve is exactly how inheritance in JPA works.
So your queries are perfectly fine and you will have instances of the subclasses in the result list.
Read more about JPA inheritance in one of my blog posts:
https://72.services/inheritance-in-jpa/

Hibernate One-to-One DTO-Entity population

I have 2 entities in my DB with one-to-one one directional mapping:
User and PasswordResetToken. The idea behind this is to create new token each time user requests password reset and store only the latest one.
Below are my entities:
#Entity
#Table(name = "USERS")
#Getter #Setter
public class User implements Serializable {
#Id
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.AUTO, generator = "usersSeq")
#SequenceGenerator(name = "usersSeq", sequenceName = "SEQ_USERS", allocationSize = 1)
private long id;
#Column(name = "NAME")
private String name;
#Column(name = "PASSWORD")
private String password;
#Column(name = "EMAIL")
private String email;
#Column(name = "ROLE")
private Integer role;
}
///...
#Entity
#Table(name = "PASSWORD_RESET_TOKENS")
#Getter
#Setter
public class PasswordResetToken implements Serializable {
private static final int EXPIRATION = 24;
#Column(name = "TOKEN")
private String token;
#Id
#OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(nullable = false, name = "user_id")
private User user;
#Column(name = "EXPIRY_DATE")
private Instant expiryDate;
public PasswordResetToken() {
}
public void setExpiryDate(ZonedDateTime expiryDate) {
this.expiryDate = expiryDate.plus(EXPIRATION, ChronoUnit.HOURS).toInstant();
}
}
Also, I have DTOs created for both of them to pass them around my app.
Code snippets:
#Getter #Setter
public class PasswordResetTokenModel {
private String token;
private ZonedDateTime expiryDate;
private UserModel user;
}
UserModel is also used for Spring Security
#Getter
#Setter
public class UserModel extends User {
public UserModel(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
private long id;
private String name;
public String getEmail() {
return this.getUsername();
}
}
For population I've created 2 populators:
#Component
public class UserPopulatorImpl implements UserPopulator {
#Autowired
UserDetailsService userDetailsService;
#Override
public UserModel populateToDTO(User user) {
UserModel userModel = new UserModel(user.getEmail(), user.getPassword(), userDetailsService.getAuthorities(user.getRole()));
userModel.setId(user.getId());
return userModel;
}
#Override
public User populateToDAO(UserModel userModel) {
User user = new User();
user.setEmail(userModel.getEmail());
user.setName(userModel.getName());
user.setPassword(userModel.getPassword());
//TODO: change it!
user.setRole(1);
return user;
}
}
//...
#Component
public class PasswordResetTokenPopulatorImpl implements PasswordResetTokenPopulator {
#Autowired
UserPopulator userPopulator;
#Override
public PasswordResetTokenModel populateToDTO(PasswordResetToken passwordResetToken) {
PasswordResetTokenModel passwordResetTokenModel = new PasswordResetTokenModel();
passwordResetTokenModel.setUser(userPopulator.populateToDTO(passwordResetToken.getUser()));
passwordResetTokenModel.setToken(passwordResetToken.getToken());
passwordResetTokenModel.setExpiryDate(ZonedDateTime.ofInstant(passwordResetToken.getExpiryDate(), ZoneId.systemDefault()));
return passwordResetTokenModel;
}
#Override
public PasswordResetToken populateToDAO(PasswordResetTokenModel passwordResetTokenModel) {
PasswordResetToken passwordResetToken = new PasswordResetToken();
passwordResetToken.setExpiryDate(passwordResetTokenModel.getExpiryDate());
passwordResetToken.setUser(userPopulator.populateToDAO(passwordResetTokenModel.getUser()));
passwordResetToken.setToken(passwordResetTokenModel.getToken());
return passwordResetToken;
}
}
I'm saving object using
sessionFactory.getCurrentSession().saveOrUpdate(token);
When I use this code, I'm getting following exception
object references an unsaved transient instance - save the transient instance before flushing: com.demo.megaevents.entities.User
There are currently 2 issues in this code:
Seems like Cascade.ALL in my OneToOne mapping is not working. If
I create separate primary key in Token class everything works almost
as expected but storing every created token in DB (more like
OneToMany relation), however I want to avoid it as I need to store
only one token per user in my DB
I don't like using new in populators, as it forces hibernate to create new object while flushing session. However, I also don't want to do another select to fetch this data from DB because just before mentioned populator I already do this query to fetch it and I think that it's an overhead.
Also, I really want to have DTOs and I don't want to remove DTO layer.
So, my questions:
What is the correct way to handle population between DTO and entities?
Are there any other improvements (probably architectural) to my solution?
Thanks a lot.
I'm not sure why you would let UserModel extend User, but I guess you did that because you didn't want to have to copy all properties from User into UserModel. Too bad, because that's what is going to be needed to have a clean separation between the entity model and data transfer model.
You get that exception because you try to persist a PasswordResetToken that has a reference to a User object with an id, but the User isn't associated with the current session. You don't have to query the user, but at least association it with the session like this:
PasswordResetToken token = // wherever you get that from
Session s = sessionFactory.getCurrentSession();
token.setUser(s.load(User.class, token.getUser().getId());
s.persist(token);
Cascading would cause the User to be created/inserted or updated via a SQL INSERT or UPDATE statement which is apparently not what you want.
You could do the Session.load() call in you populators if you want, but I'd not do that. Actually I would recommend not having populators at all, but instead create the entity objects in your service instead.
Normally you only have a few(mostly 1) ways of actually creating a new entity object, so the full extent of the transformation from DTO to entity will only be relevant in very few cases.
Most of the time you are going to do an update and for that, you should first select the existing entity and apply the fields that are allowed to be changed from the DTO on the entity object.
For providing the presentation layer with DTOs I would recommend using Blaze-Persistence Entity Views to avoid the manual mapping boilerplate and also improve performance of select queries.

How to make a query to get results based on two othe relations in JPA? (kinda join three tables)

I have a Post class which is like below and it has a one-to-many relation with Users (people who can see this post) and also it has the same kind of relation (one-to-many) with Groups, now I want to get all the posts by a JPA Query based on Users and Groups, how should I write the JPA Query for this?
/* Post */
public class Post extends Model {
private String title;
private String text;
#OneToMany
#JoinTable(
name="tbl_post_users",
inverseJoinColumns={#JoinColumn(name="userId")},
joinColumns={#JoinColumn(name="postId")}
)
public List<User> users;
#OneToMany
#JoinTable(
name="tbl_post_groups",
inverseJoinColumns={#JoinColumn(name="groupId")},
joinColumns={#JoinColumn(name="postId")}
)
public List<Group> groups;
public static List<Post> GetPosts(){
// JPA Query here
}
}
/* User */
public class User extends Model {
#Id
private int id;
private String name;
private String email;
}
/* Group */
public class Group extends Model {
#Id
private int id;
private String title;
private int ownerId;
}
So I want to write the query so that I can get a list of all posts where user is {user} or group is {group}
select distinct p from Post p
left join p.users user
left join p.groups group
where user = :user
or group = :group
Read http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-joins

Categories

Resources