Skip to content

Instantly share code, notes, and snippets.

@jnorthrup
Created March 10, 2011 14:33
Show Gist options
  • Save jnorthrup/864175 to your computer and use it in GitHub Desktop.
Save jnorthrup/864175 to your computer and use it in GitHub Desktop.
package com.talkwheel.model;
import java.util.List;
import java.util.concurrent.Callable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
import javax.persistence.Version;
import com.google.inject.Inject;
import com.talkwheel.server.rfactory.KernelImpl;
//import org.springframework.beans.factory.annotation.Configurable;
//import org.springframework.roo.addon.entity.RooEntity;
//import org.springframework.roo.addon.javabean.RooJavaBean;
//import org.springframework.roo.addon.tostring.RooToString;
//import org.springframework.transaction.annotation.Transactional;
@SuppressWarnings({"JpaQlInspection"})
//@Configurable
@Entity
//@RooJavaBean
//@RooToString(toStringMethod = "rooToString")
//@RooEntity
public class TwAttachment {
private String name;
private String type;
private String url;
private Integer len;
private Long id;
Integer version;
@com.google.inject.Inject
transient private EntityManager entityManager;
public static final EntityManager entityManager() {
return KernelImpl.INJECTOR.getInstance(EntityManager.class);
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getLen() {
return this.len;
}
public void setLen(Integer len) {
this.len = len;
}
public String rooToString() {
StringBuilder sb = new StringBuilder();
sb.append("Name: ").append(getName()).append(", ");
sb.append("Type: ").append(getType()).append(", ");
sb.append("Url: ").append(getUrl()).append(", ");
sb.append("Len: ").append(getLen());
return sb.toString();
}
/*
@PersistenceContext
@com.google.inject.Inject
transient EntityManager entityManager;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Version
@Column(name = "version")
Integer version;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getVersion() {
return this.version;
}
public void setVersion(Integer version) {
this.version = version;
}
//@com.google.inject.persist.Transactional
public void persist() {
ModelTx.persist(this);
}
//@com.google.inject.persist.Transactional
public void remove() {
ModelTx.remove(this, TwAttachment.class);
}
//@com.google.inject.persist.Transactional
public void flush() {
ModelTx.flush();
}
//@com.google.inject.persist.Transactional
public TwAttachment merge() {
return ModelTx.merge(this);
}
public static final EntityManager entityManager() {
return ModelTx.entityManager();
}
*/
public static long countTwAttachments() {
return TwItem.entityManager().createQuery("select count(o) from TwAttachment o", Long.class).getSingleResult();
}
public static List<TwAttachment> findAllTwAttachments() {
return TwItem.entityManager().createQuery("select o from TwAttachment o", TwAttachment.class).getResultList();
}
public static TwAttachment findTwAttachment(Long id) {
if (id == null) return null;
return TwItem.entityManager().find(TwAttachment.class, id);
}
public static List<TwAttachment> findTwAttachmentEntries(int firstResult, int maxResults) {
return TwItem.entityManager().createQuery("select o from TwAttachment o", TwAttachment.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Version
@Column(name = "version")
public Integer getVersion() {
return this.version;
}
public void setVersion(Integer version) {
this.version = version;
}
public void persist() {
if (this.getEntityManager() == null)
this.setEntityManager(TwItem.entityManager());
this.getEntityManager().persist(this);
}
//@com.google.inject.persist.Transactional
public <T> void remove() {
if (this.getEntityManager() == null)
this.setEntityManager(TwItem.entityManager());
if (this.getEntityManager().contains(this)) {
this.getEntityManager().remove(this);
} else {
T attached = getEntityManager().<T>find((Class<T>) getClass(), this.getId());
this.getEntityManager().remove(attached);
}
}
//@com.google.inject.persist.Transactional
public void flush() {
if (this.getEntityManager() == null)
this.setEntityManager(TwItem.entityManager());
this.getEntityManager().flush();
}
//@com.google.inject.persist.Transactional
public <T> T merge() {
if (this.getEntityManager() == null)
this.setEntityManager(TwItem.entityManager());
T merged = (T) this.getEntityManager().merge(this);
this.getEntityManager().flush();
return merged;
}
@Transient
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
private static class EntityManagerCallable implements Callable<EntityManager> {
@Inject
EntityManager em;
@Override
public EntityManager call() throws Exception {
return em;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment