Skip to content

Instantly share code, notes, and snippets.

View hseritt's full-sized avatar
🏠
Working from home

Harlin Seritt hseritt

🏠
Working from home
View GitHub Profile
@hseritt
hseritt / AnyClass.java
Created June 27, 2016 01:51
Create a Logger object
private static Logger logger = Logger.getLogger(BaseAgent.class);
@hseritt
hseritt / Controller.java
Created June 27, 2016 01:52
Dynamic class instantiation using Thread
public class Controller implements Runnable {
private static Logger logger = Logger.getLogger(Controller.class);
public void run() {
logger.info("Starting subsystem AgentController");
String sql = "select * from agent where active=true";
@hseritt
hseritt / DbConnector.java
Created June 27, 2016 01:54
Creating a database connection and using properties files
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.log4j.Logger;
@hseritt
hseritt / Agent.java
Created June 27, 2016 01:54
Jpa Table and Model (unique constraints)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@hseritt
hseritt / StartPageProperties.java
Created June 27, 2016 01:55
Properties configuration file
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("web.start")
public class StartPageProperties {
private String appName;
private String pageTitle;
private String templateFolder;
@hseritt
hseritt / AgentRepository.java
Created June 27, 2016 01:56
JPA Repository
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AgentRepository extends JpaRepository<Agent, Long> {
List<Agent> findByActive(boolean active);
}
@hseritt
hseritt / Start.java
Created June 27, 2016 01:57
Spring MVC controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class Start {
@hseritt
hseritt / application.properties
Created June 27, 2016 01:57
Spring boot application.properties file
# Security
security.basic.enabled=false
management.security.enabled=false
# Database settings
spring.datasource.url=jdbc:mysql://localhost/alfmonitor
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.hibernate.ddl-auto=update
@hseritt
hseritt / HibernateUtil.java
Created June 27, 2016 02:04
HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
@hseritt
hseritt / AgentQueue.java
Created June 27, 2016 02:16
How to handle opening and closing Hibernate sessions
Session session = HibernateUtil.getSessionFactory().openSession();
session.setFlushMode(FlushMode.MANUAL);
ManagedSessionContext.bind(session);
session.beginTransaction();
Query getAgentsQuery = session.createQuery(" from AgentModel where active=true");
@SuppressWarnings("unchecked")
List<AgentModel> agentList = getAgentsQuery.list();
for (AgentModel agentModel : agentList) {