Skip to content

Commit

Permalink
Use hibernate better
Browse files Browse the repository at this point in the history
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 c8dcd10
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/schmal/dao/CategoryDAO.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 13 additions & 15 deletions src/main/java/com/schmal/dao/WeekDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/schmal/domain/Matchup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,7 @@

@NoArgsConstructor
@RequiredArgsConstructor
@EqualsAndHashCode(exclude = {"ID"})
@Entity
@Table(name = "matchup")
public class Matchup
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/schmal/domain/Week.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/schmal/service/WeekService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -27,6 +28,8 @@ public class WeekService
{
private final WeekDAO dao;

private final LeagueDAO leagueDAO;

private final LeagueService leagueService;

private final TeamService teamService;
Expand All @@ -38,6 +41,7 @@ public class WeekService
public WeekService(HibernateBundle hibernateBundle)
{
dao = new WeekDAO(hibernateBundle.getSessionFactory());
leagueDAO = new LeagueDAO(hibernateBundle.getSessionFactory());
teamService = new TeamService(hibernateBundle);
leagueService = new LeagueService(hibernateBundle);
}
Expand All @@ -58,12 +62,7 @@ public List<Week> createWeeks(long leagueID) throws Exception
break;
}

if (weeks.size() > 0)
{
dao.save(weeks);
}

return weeks;
return dao.save(league, weeks);
}

private List<Week> createESPNWeeks(
Expand Down

0 comments on commit c8dcd10

Please sign in to comment.