Permalink
Browse files

Use hibernate better

Been playing around with hibernate. An HQL delete query is not the way
to do things; it's better to manipulate the objects and let Hibernate do
everything for you.
  • Loading branch information...
tschmal committed Apr 21, 2013
1 parent 9e57140 commit c8dcd10e264198c94b88065f311043d20dcfb895
View
@@ -72,7 +72,7 @@ Until I can get the dropwizard-migrations and Liquibase behavior working correct
CREATE TABLE week
(
id integer NOT NULL,
- league_id integer NOT NULL,
+ league_id integer REFERENCES league(id) ON DELETE CASCADE NOT NULL,
start_date date NOT NULL,
end_date date NOT NULL,
PRIMARY KEY (id),
@@ -0,0 +1,24 @@
+package com.schmal.dao;
+
+import com.schmal.domain.Category;
+import com.yammer.dropwizard.hibernate.AbstractDAO;
+import java.util.List;
+import org.hibernate.SessionFactory;
+
+public class CategoryDAO extends AbstractDAO<Category>
+{
+ public CategoryDAO(SessionFactory factory)
+ {
+ super(factory);
+ }
+
+ public List<Category> save(List<Category> categories)
+ {
+ for (Category category : categories)
+ {
+ persist(category);
+ }
+
+ return categories;
+ }
+}
@@ -8,28 +8,26 @@
public class WeekDAO extends AbstractDAO<Week>
{
+ private final LeagueDAO leagueDAO;
+
public WeekDAO(SessionFactory factory)
{
super(factory);
+
+ leagueDAO = new LeagueDAO(factory);
}
- public List<Week> save(List<Week> weeks)
+ public List<Week> save(League league, List<Week> weeks)
{
- if (weeks.size() > 0)
- {
- deleteWeeks(weeks.get(0).getLeague());
- }
+ // Delete everything.
+ league.getWeeks().clear();
+ leagueDAO.save(league);
+ currentSession().flush();
- for (Week week : weeks)
- {
- persist(week);
- }
+ // Insert all the new Weeks.
+ league.getWeeks().addAll(weeks);
+ leagueDAO.save(league);
- return weeks;
- }
-
- public void deleteWeeks(League league)
- {
- namedQuery("deleteByLeagueID").setParameter("league", league).executeUpdate();
+ return league.getWeeks();
}
}
@@ -7,6 +7,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.OneToOne;
import javax.persistence.Table;
+import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
@@ -16,6 +17,7 @@
@NoArgsConstructor
@RequiredArgsConstructor
+@EqualsAndHashCode(exclude = {"ID"})
@Entity
@Table(name = "matchup")
public class Matchup
@@ -8,11 +8,10 @@
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
-import javax.persistence.NamedQueries;
-import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
+import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
@@ -22,14 +21,9 @@
@NoArgsConstructor
@RequiredArgsConstructor
+@EqualsAndHashCode(exclude = {"ID", "matchups"})
@Entity
@Table(name = "week")
-@NamedQueries({
- @NamedQuery(
- name = "deleteByLeagueID",
- query = "delete from Week where league = :league"
- )
-})
public class Week
{
@Id
@@ -1,5 +1,6 @@
package com.schmal.service;
+import com.schmal.dao.LeagueDAO;
import com.schmal.dao.WeekDAO;
import com.schmal.domain.League;
import com.schmal.domain.Matchup;
@@ -27,6 +28,8 @@
{
private final WeekDAO dao;
+ private final LeagueDAO leagueDAO;
+
private final LeagueService leagueService;
private final TeamService teamService;
@@ -38,6 +41,7 @@
public WeekService(HibernateBundle hibernateBundle)
{
dao = new WeekDAO(hibernateBundle.getSessionFactory());
+ leagueDAO = new LeagueDAO(hibernateBundle.getSessionFactory());
teamService = new TeamService(hibernateBundle);
leagueService = new LeagueService(hibernateBundle);
}
@@ -58,12 +62,7 @@ public WeekService(HibernateBundle hibernateBundle)
break;
}
- if (weeks.size() > 0)
- {
- dao.save(weeks);
- }
-
- return weeks;
+ return dao.save(league, weeks);
}
private List<Week> createESPNWeeks(

0 comments on commit c8dcd10

Please sign in to comment.