Skip to content

Commit

Permalink
Manually inserting in IDs... there has to be a better way
Browse files Browse the repository at this point in the history
  • Loading branch information
feminaexlux committed Sep 3, 2013
1 parent 146eb68 commit 251ed16
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@

import net.feminaexlux.gallery.struts2.model.Album;

import javax.persistence.EntityManager;

public class AlbumDAO extends ResourceDAO<Album> {
@Override
protected void saveNew(Album album, EntityManager entityManager) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@

import net.feminaexlux.gallery.struts2.model.Image;

import javax.persistence.EntityManager;

public class ImageDAO extends ResourceDAO<Image> {
@Override
protected void saveNew(Image resource, EntityManager entityManager) {
}
}
20 changes: 14 additions & 6 deletions src/main/java/net/feminaexlux/gallery/struts2/dao/ResourceDAO.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.feminaexlux.gallery.struts2.dao;

import net.feminaexlux.gallery.struts2.model.Resource;
import net.feminaexlux.gallery.struts2.model.ResourceKey;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.*;
Expand All @@ -21,8 +20,14 @@ public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

public T find(Class<T> clazz, ResourceKey key) {
return entityManager.find(clazz, key);
public T find(Class<T> clazz, int id, String type) {
TypedQuery<T> query = entityManager.createQuery("FROM " + clazz.getSimpleName() + " t " +
"WHERE t.id = :id AND t.type = :type", clazz);

query.setParameter("id", id);
query.setParameter("type", type);

return query.getSingleResult();
}

public List<T> findAll(Class<T> clazz) {
Expand All @@ -37,7 +42,12 @@ public T save(T resource) {

try {
if (resource.getId() == 0) {
saveNew(resource, entityManager);
// FIXME: hacky way of doing things
Query query = entityManager.createNativeQuery("SELECT MAX(resource_id) FROM resource");
int id = (Integer) query.getSingleResult();
resource.setId(id + 1);

entityManager.persist(resource);
} else {
entityManager.merge(resource);
}
Expand All @@ -51,6 +61,4 @@ public T save(T resource) {

return resource;
}

protected abstract void saveNew(T resource, EntityManager entityManager);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.feminaexlux.gallery.struts2.model.User;

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

public class UserDAO extends ResourceDAO<User> {
Expand All @@ -17,8 +16,4 @@ public User findByUsername(String login) {
public User save(User user) {
return super.save(user);
}

@Override
protected void saveNew(User resource, EntityManager entityManager) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Album extends Resource implements Linkable {
private String slug;

public Album() {
this.key = new ResourceKey(0, ResourceType.ALBUM);
this.type = ResourceType.ALBUM;
}

public Album getParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public class Image extends Resource implements Linkable {
private String slug;

public Image() {
this.key = new ResourceKey(0, ResourceType.IMAGE);
}

public Image(String type) {
this.key = new ResourceKey(0, type);
this.type = ResourceType.IMAGE;
}

public Album getAlbum() {
Expand Down
44 changes: 21 additions & 23 deletions src/main/java/net/feminaexlux/gallery/struts2/model/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
import org.hibernate.annotations.DiscriminatorOptions;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "resource")
@Inheritance
@DiscriminatorColumn(name = "resource_type")
@DiscriminatorOptions(insert = false)
public class Resource {
@EmbeddedId
protected ResourceKey key;
public class Resource implements Serializable {
@Id
@Column(name = "resource_id")
protected int id;

@Id
@Column(name = "resource_type", length = 50)
protected String type;

@Column(name = "resource_name", nullable = false, length = 50)
protected String name;
Expand All @@ -29,30 +35,20 @@ public class Resource {
@Temporal(TemporalType.TIMESTAMP)
protected Date deleted;

public ResourceKey getKey() {
return key;
}

public void setKey(ResourceKey key) {
this.key = key;
}

@Transient
public int getId() {
if (key == null) {
return 0;
}
return id;
}

return key.getId();
public void setId(int id) {
this.id = id;
}

@Transient
public String getType() {
if (key == null) {
return null;
}
return type;
}

return key.getType();
public void setType(String type) {
this.type = type;
}

public String getName() {
Expand Down Expand Up @@ -94,18 +90,20 @@ public boolean equals(Object o) {

Resource resource = (Resource) o;

if (id != resource.id) return false;
if (created != null ? !created.equals(resource.created) : resource.created != null) return false;
if (deleted != null ? !deleted.equals(resource.deleted) : resource.deleted != null) return false;
if (key != null ? !key.equals(resource.key) : resource.key != null) return false;
if (name != null ? !name.equals(resource.name) : resource.name != null) return false;
if (type != null ? !type.equals(resource.type) : resource.type != null) return false;
if (updated != null ? !updated.equals(resource.updated) : resource.updated != null) return false;

return true;
}

@Override
public int hashCode() {
int result = key != null ? key.hashCode() : 0;
int result = id;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (created != null ? created.hashCode() : 0);
result = 31 * result + (updated != null ? updated.hashCode() : 0);
Expand Down

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/java/net/feminaexlux/gallery/struts2/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class User extends Resource {
@Column(table = "user", name = "user_password", nullable = false, length = 50)
private String password;

public User() {
this.type = ResourceType.USER;
}

public String getLogin() {
return login;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import net.feminaexlux.gallery.struts2.dao.ResourceDAO;
import net.feminaexlux.gallery.struts2.model.Linkable;
import net.feminaexlux.gallery.struts2.model.Resource;
import net.feminaexlux.gallery.struts2.model.ResourceKey;
import net.feminaexlux.gallery.struts2.utility.StringUtility;

import java.util.Date;

public abstract class ResourceService<T extends Resource> {
public T get(int id, String type, Class<T> resourceClass) {
return dao().find(resourceClass, new ResourceKey(id, type));
return dao().find(resourceClass, id, type);
}

public T save(T resource) {
Expand Down
1 change: 1 addition & 0 deletions web/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<property name="hibernate.hbm2ddl.auto" value=""/>
<property name="hibernate.max_fetch_depth" value="1"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
</properties>
</persistence-unit>
</persistence>

0 comments on commit 251ed16

Please sign in to comment.