Saturday, February 27, 2016

JAVA JPA Goal Repository

JAVA JPA
//Repository
package com.thekhuc.repository;

import java.util.List;

import com.thekhuc.model.Goal;
import com.thekhuc.model.GoalReport;

public interface GoalRepository {

Goal save(Goal goal);

List<Goal> loadAll();

List<GoalReport> findAllGoalReports();

}

//Interface
package com.thekhuc.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.thekhuc.model.Goal;
import com.thekhuc.model.GoalReport;

@Repository("goalRepository")
public class GoalRepositoryImpl implements GoalRepository {

@PersistenceContext
private EntityManager em;

@Override
public Goal save(Goal goal) {
if(goal.getId() == null){
em.persist(goal);
em.flush();
}
else{
goal = em.merge(goal);
}
return goal;
}

@Override
public List<Goal> loadAll() {
//Query query=em.createQuery("Select g from Goal g");

TypedQuery<Goal> query = em.createNamedQuery(Goal.FIND_ALL_GOALS, Goal.class);
return query.getResultList();
}

@Override
public List<GoalReport> findAllGoalReports() {
//Query query = em.createQuery("Select new com.thekhuc.model.GoalReport(g.minutes, e.minutes, e.activity) " +
// "from Goal g, Exercise e where g.id = e.goal.id");

TypedQuery<GoalReport> query = em.createNamedQuery(Goal.FIND_GOAL_REPORTS, GoalReport.class);
return query.getResultList();
}

}

Sunday, February 21, 2016

persistence.xml

/src/main/resources/META-INF/persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.or/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/XML/ns/persistence http://java.sun.com/xml/ns/persistence_2_0.xsd"
version="2.0">

<persistence-unit name="punit"></persistence-unit>

</persistence>

jpaContext.xml

/src/main/resources/jpaContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<context:annotation-config/>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<entry key="hibernate.hbm2ddl.auto" value="create"/>
<entry key="hibernate.format_sql" value="true"/>
</map>
</property>
</bean>

<bean id="transactionmanager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/fitnesstracker?autoReconnect=true"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>

</beans>

Installing MySQL Community Server 5.7.11 and Python 3.5.1




Friday, February 19, 2016