Feeds:
Posts
Comments

Archive for July, 2017

jpa locking

some good introduction to different locking types:

http://lostincoding.blogspot.com/2015/11/differences-in-jpa-entity-locking-modes.html

http://www.byteslounge.com/tutorials/locking-in-jpa-lockmodetype

https://stackoverflow.com/questions/13581603/jpa-and-optimistic-locking-modes

https://docs.oracle.com/javaee/7/tutorial/persistence-locking001.htm

Read Full Post »

        <subsystem xmlns=”urn:jboss:domain:transactions:3.0″>

            <core-environment node-identifier=”younameit”>

                <process-id>

                    <uuid/>

                </process-id>

            </core-environment>

            <recovery-environment socket-binding=”txn-recovery-environment” status-socket-binding=”txn-status-manager”/>

        </subsystem>

in standalone.xml add node-identifier=”younameit” to core-environment

Read Full Post »

Application folder structure:
-your-application

    -your-application-ear
    -your-application-ejb
    -your-application-web

Maven dependency management in your-application/pom.xml:

<dependencyManagement>

<dependencies>
<!– https://mvnrepository.com/artifact/org.jboss.shrinkwrap.resolver/shrinkwrap-resolver-impl-maven –>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-implmaven</artifactId>

<version>2.2.6</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-apimaven</artifactId>

<version>2.2.6</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-api</artifactId>

<version>2.2.6</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-spimaven</artifactId>

<version>2.2.6</version>

<scope>test</scope>

</dependency>

<!– https://mvnrepository.com/artifact/org.jboss.shrinkwrap.resolver/shrinkwrap-resolver-spi –>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-spi</artifactId>

<version>2.2.6</version>

</dependency>

</dependencies>

</dependencyManagement>

 

Maven dependencies in your-application-ejb/pom.xml:
<
dependencies>

<dependency>

<groupId>org.jboss.arquillian.junit</groupId>

<artifactId>arquillianjunit-container</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.jboss.arquillian.protocol</groupId>

<artifactId>arquillian-protocol-servlet</artifactId>

<scope>test</scope>

</dependency>

<!–exclude default older versions of dependencies coming with the lib–>
<
dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-implmaven</artifactId>

<scope>test</scope>

<exclusions>

<exclusion>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-spi</artifactId>

</exclusion>

<exclusion>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-spimaven</artifactId>

</exclusion>

</exclusions>

</dependency>
<!–include newer versions of dependencies after excluding older ones before–>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-apimaven</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-api</artifactId>

<scope>test</scope>

</dependency>

<!– https://mvnrepository.com/artifact/org.jboss.shrinkwrap.resolver/shrinkwrap-resolver-spi –>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-spimaven</artifactId>

<exclusions>

<exclusion>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-spi</artifactId>

</exclusion>

</exclusions>

</dependency>

<!– https://mvnrepository.com/artifact/org.jboss.shrinkwrap.resolver/shrinkwrap-resolver-spi –>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-spi</artifactId>

</dependency>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-implmaven-archive</artifactId>

<scope>test</scope>

</dependency>
</dependencies>

Enable remote container test in your-application-ejb/pom.xml:

<profiles>

<profile>

<!– The default profile skips all tests, though you can tune it to run

just unit tests based on a custom pattern –>

<!– Seperate profiles are provided for running all tests, including Arquillian

tests that execute in the specified container –>

<id>default</id>

<!– <activation> <activeByDefault>true</activeByDefault> </activation> –>

<build>

<plugins>

<plugin>

<artifactId>mavensurefireplugin</artifactId>

<version>${version.surefire.plugin}</version>

<configuration>

<skip>true</skip>

</configuration>

</plugin>

</plugins>

</build>

</profile>

<profile>

<!– An optional Arquillian testing profile that executes tests in your

WildFly instance –>

<!– This profile will start a new WildFly instance, and execute the test,

shutting it down when done –>

<!– Run with: mvn clean test –Parqwildfly-managed –>

<id>arqwildfly-managed</id>

<dependencies>

<dependency>

<groupId>org.wildfly</groupId>

<artifactId>wildflyarquillian-container-managed</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

</profile>

<profile>

<!– An optional Arquillian testing profile that executes tests in a remote

WildFly instance –>

<!– Run with: mvn clean test –Parqwildfly-remote –>

<id>arqwildfly-remote</id>

<!–enable this one–>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

<dependencies>

<dependency>

<groupId>org.wildfly</groupId>

<artifactId>wildflyarquillian-container-remote</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

</profile>

</profiles>

Create tests in src/test/java in your-application-ejb:

@RunWith(Arquillian.class)

public class YourTest {

@Inject

private TestService testService;

private static Logger log = Logger.getLogger(TestService.class.getName());

@Deployment

public static Archive<?> createDeploymentFromPom() {

//pay attention here: which pom.xml file is loaded

        PomEquippedResolveStage mavenResolver = Maven.configureResolver().loadPomFromFile(“../your-application-ear/pom.xml”);

        EnterpriseArchive archive = mavenResolver.resolve(“G:A:ear:V”)

                .withoutTransitivity().as(EnterpriseArchive.class)[0];

JavaArchive testLibraryHelper = ShrinkWrap.create(JavaArchive.class)

        archive.addAsLibrary(testLibraryHelper);

return archive;

}

@Test

@RunAsClient//This tells Arquillian to run the test as a client against the remote server and not within the remote server.

    public void testBeanInstance() {

System.out.print(“run as-client test”);

    }

@Test

    public void testTestService() {

log.info(“run in-container test”);

Assert.assertNotNull(testService);

    }

    

}

Read Full Post »

spring mail is in spring-context-support and the maven dependency is:

<!– https://mvnrepository.com/artifact/org.springframework/spring-context-support –>

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-support</artifactId>

    <version>4.3.9.RELEASE</version>

</dependency>

generate a CDI friendly bean with some initialization:

import java.util.Properties;

import javax.enterprise.inject.Produces;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.JavaMailSenderImpl;

public class MyMailSender {

@Produces

public JavaMailSender getInstance() {

JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

javaMailSender.setHost(“smtp.gmail.com”);

javaMailSender.setPort(465);

javaMailSender.setUsername(“yourusername”);

javaMailSender.setPassword(“yourpassword”);

javaMailSender.setProtocol(“smtps”);

Properties javaMailProperties = new Properties();

javaMailProperties.put(“mail.smtps.auth”, true);

javaMailProperties.put(“mail.smtps.starttls.enable”, true);

javaMailSender.setJavaMailProperties(javaMailProperties);

return javaMailSender;

}

}

inject the produced CDI bean and implement a sendEmail method:

import javax.inject.Inject;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.SimpleMailMessage;

public class MailSenderService {

@Inject

private JavaMailSender mailSender;

public void sendEmail(String fromEmail, String toEmail, String subject, String text) {

SimpleMailMessage mail = new SimpleMailMessage();

mail.setFrom(fromEmail);

mail.setTo(toEmail);

mail.setSubject(subject);

mail.setText(text);

mailSender.send(mail);

}

}

 

Then you can inject MailSenderService to your java class to send emails:

@Inject

private MailSenderService mailSenderService;

mailSenderService.sendEmail(“fromEmail”,“toEmail”,“email subject”,“email content”);

Read Full Post »

The problem description is here:

https://stackoverflow.com/questions/21635734/making-many-to-many-relationship-cacheable-in-jpa

The joint table will not be cached for the following ManyToMany relationship mapping:

@Cacheable

@Entity

public class User implements Serializable {

privatestaticfinallongserialVersionUID = 1L;

private Integer entryid;

private String userName;

private String password;

private Set<Role> roles;

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name=“user_id”, unique = true, nullable = false)

public Integer getEntryid() {

return entryid;

}

public void setEntryid(Integer entryid) {

this.entryid = entryid;

}

@Column(name=“user_name”, unique = true, nullable = false)

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@ManyToMany(cascade = {

    CascadeType.PERSIST,

    CascadeType.MERGE

}, fetch=FetchType.EAGER)

@JoinTable(name=“user_role”, joinColumns=@JoinColumn(name=“user_id”),inverseJoinColumns=@JoinColumn(name=“role_id”))

public Set<Role> getRole() {

if (roles == null) {

return new HashSet<>();

}

return roles;

}

public void setRole(Set<Role> roles) {

this.roles = roles;

}

}

@Cacheable

@Entity

public class Roles implements Serializable {

privatestaticfinallongserialVersionUID = 1L;

private Integer entryid;

private String roleName;

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name=“role_id”, unique = true, nullable = false)

public Integer getEntryid() {

return entryid;

}

public void setEntryid(Integer entryid) {

this.entryid = entryid;

}

@Column(name=“role_name”, unique = true, nullable = false)

public String getRoleName() {

return roleName;

}

public void setRoleName(String roleName) {

this.roleName = roleName;

}

}

 

To solve this problem, we can convert the ManyToMany mapping to ManyToOne mapping. Here is the code:

@Cacheable

@Entity

public class Users implements Serializable {

privatestaticfinallongserialVersionUID = 1L;

private Integer entryid;

private String userName;

private String password;

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name=“user_id”, unique = true, nullable = false)

public Integer getEntryid() {

return entryid;

}

public void setEntryid(Integer entryid) {

this.entryid = entryid;

}

@Column(name=“user_name”, unique = true, nullable = false)

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

} 

}

 

@Cacheable

@Entity

public class Roles implements Serializable {

privatestaticfinallongserialVersionUID = 1L;

private Integer entryid;

private String roleName;

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name=“role_id”, unique = true, nullable = false)

public Integer getEntryid() {

return entryid;

}

public void setEntryid(Integer entryid) {

this.entryid = entryid;

}

@Column(name=“role_name”, unique = true, nullable = false)

public String getRoleName() {

return roleName;

}

public void setRoleName(String roleName) {

this.roleName = roleName;

}

}

 

@Entity

@Cacheable

public class UserRole implements Serializable {

private static final long serialVersionUID = 2235414006657954864L;

@Id

    @GeneratedValue

    private Long entryid;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})

    @JoinColumn(name = “user_id”)

    private User user;

    

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})

    @JoinColumn(name = “role_id”)

    private Role role;

public Long getEntryid() {

return entryid;

}

public void setEntryid(Long entryid) {

this.entryid = entryid;

}

public User getUser() {

returnuser;

}

public void setUser(User user) {

this.user = user;

}

public Roles getRole() {

returnrole;

}

public void setRole(Role role) {

this.role = role;

}

    

    

}

Read Full Post »