Introduction :
First, to explore the Java EE standard , I'll start by writing an article to simplify the development of applications that rely on Java EE 7.Now, let's configure :
To begin, we will create a maven project and to do so, we can use the plugin maven below :
mvn -DarchetypeGroupId=org.wildfly.archetype -DarchetypeArtifactId=wildfly-javaee7-webapp-blank-archetype -DarchetypeVersion=8.2.0.Final -DgroupId=fr.blogspot.mjhazbri -DartifactId=ContactManagement -Dversion=1.0.0-SNAPSHOT -Dpackage=fr.blogspot.mjhazbri -Darchetype.interactive=false --batch-mode --update-snapshots archetype:generate
Framework :
The list of frameworks that will be used is EJB3.2 , JPA2.1.
Generated file :
Generated file :
There is a portion of source code and configuration that are generated and which are :
persistence.xml
This datasource refers to the RDBMS MySQL then the driver must be provided and also create the associated database , so the driver can be downloaded from internet and we must put it under the deployments directory of our Wildfly server.
To create the database , simply go on MySQL Workbench and execute the following query
persistence.xml
If you notice well , we see a datasource was declared , the data dource is under the WEB- INF folder in the webapp folder :java:jboss/datasources/ContactsDS
jdbc:mysql://localhost:3306/contacts mysql root
This datasource refers to the RDBMS MySQL then the driver must be provided and also create the associated database , so the driver can be downloaded from internet and we must put it under the deployments directory of our Wildfly server.
To create the database , simply go on MySQL Workbench and execute the following query
create database contacts;
Now, let's code :
The first thing to do is to code entities , I'll create three entities User, Group and Right :
package fr.blogspot.mjhazbri.entities; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; /** * @author jhazbri * */ @Entity @Table(name = "USERS") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "usr_login") private String login; @Column(name = "usr_password") private String password; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "usr_group") private Group group; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } }
package fr.blogspot.mjhazbri.entities; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table; /** * @author jhazbri * */ @Entity @Table(name = "GROUPS") public class Group { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "grp_name") private String groupName; @OneToMany(targetEntity = User.class, mappedBy = "group") private Setusers = new HashSet (); @ManyToMany(targetEntity = Right.class) private Set rights = new HashSet (); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } public Set getUsers() { return users; } public void setUsers(Set users) { this.users = users; } public Set getRights() { return rights; } public void setRights(Set rights) { this.rights = rights; } }
package fr.blogspot.mjhazbri.entities; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; /** * @author jhazbri * */ @Entity @Table(name = "RIGHTS") public class Right { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "rht_name") private String rightValue; @ManyToMany(targetEntity = Group.class, mappedBy = "rights") private Setgroups = new HashSet (); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRightValue() { return rightValue; } public void setRightValue(String rightValue) { this.rightValue = rightValue; } public Set getGroups() { return groups; } public void setGroups(Set groups) { this.groups = groups; } }
package fr.blogspot.mjhazbri.dao; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import fr.blogspot.mjhazbri.entities.User; /** * @author jhazbri * */ @Stateless public class UserDao { private final static Logger logger = Logger.getLogger(UserDao.class .getName()); @PersistenceContext() private EntityManager em; @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) @PostConstruct public void init() { logger.info("init EJB method ... "); } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) @PreDestroy public void destroy() { logger.info("destory EJB method ... "); } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public User create(User user) { em.persist(user); return user; } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public User update(User user) { em.merge(user); return user; } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public User search(Integer userId) { return em.find(User.class, userId); } }
So to test the injection of EJB , I create a service where I will inject Dao :
/** * */ package fr.blogspot.mjhazbri.services; import javax.ejb.EJB; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import fr.blogspot.mjhazbri.dao.UserDao; import fr.blogspot.mjhazbri.entities.Group; import fr.blogspot.mjhazbri.entities.User; /** * @author jhazbri * */ @Stateless public class UserManagementService { @EJB private UserDao userDao; @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) private User addUserInGroup(User user, Group group) { user.setGroup(group); return userDao.create(user); } }
Aucun commentaire :
Enregistrer un commentaire