Skip to main content

Posts

Showing posts with the label hibernate

Is Hibernate an overkill for an Android application?

I'm looking for a good ORM for my android application and at first glance it seems like for a mobile device I would prefer to use something simpler maybe. The thing is I'm just assuming here with no real evidence, so I thought I would ask the community's opinion (maybe there's is someone that has been through the experience). It is a fairly large(for mobile) application and will be run on a dedicated tablet. What does everyone else think ? Is Hibernate too much for an android application ? Will there be performance problems ? What would you use instead if you think it is too much ? I am aware there are other questions asking for alternatives, but I decided to ask since most of those questions simply assumed it was an overkill and asked for other options and I started wondering "Is it really and overkill ? Why ?" Due to my lack of experience I simply think it it, but can't really provide an answer if I'm asked to explain why. Is it performance ?

apache commons equals/hashcode builder

I'm curious to know, what people here think about using org.apache.commons.lang.builder EqualsBuilder/HashCodeBuilder for implementing the equals/hashcode? Would it be a better practice than writing your own? Does it play well with Hibernate? What's your opinion?

How to auto insert Current DATE in SQL with Java / Hibernate

I need to add automatically the current date into my Database when I create a new OperantionBank. I'm using Hibernate. Thanks import java.io.Serializable; import java.sql.Date; import javax.persistence.*; import org.hibernate.annotations.Generated; import org.hibernate.annotations.GenerationTime; @Entity public class OperationBank implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String wordring; private Double amount; @Generated(GenerationTime.ALWAYS) @Temporal(javax.persistence.TemporalType.DATE) private Date dateoperation = new java.sql.Date(new java.util.Date().getTime()); @OneToOne private Account account;

Remove an object from session in hibernate

How can I remove an object from hibernate session? In my code I am fetching one list from the DB and searching if the user entered data is already there in the DB. If it is there I am overwriting it. Else I am creating a new object and saving it. But the jboss shows error that there are two objects in session. As far as i guess one object is the object that iterates through the list and other is the one newly created for saving the data. for(Allocation al: allocatelist){ if(al.getDate().compareTo(dt)==0){ al.setAllocated(gpsz); getManager().save(al); flag=1; break; { { If the above condition fails then am creating and new object and saving it. So is there any way by which I can remove this object "al"? I don't have merger or update methods I have tried "evict()" also but its of no use. The else block Allocation allocate = new Allocation(); allocate = filldata(allocate, dataMap,i); getManager().save(allocate)

Using DISTINCT keyword in JPA on individual columns

I am reading some values from a database that is horribly un-normalized (which I can't control). The call retrieves announcements for university departments, and if a user is in multiple departments (which is possible), then the same results are returned multiple times for these users. However, some departments might have different announcements, while some have the same. Is there a way for me to use the DISTINCT keyword in JPA on individual columns? This is what I currently have for the query: String jpql = "SELECT DISTINCT annoucement FROM Announcment announcement " + "WHERE (announcement.date <= :now AND announcement.endDate >= :now) " + "AND announcement.approved = true AND announcement.departmentId IN (:departmentIDs)"; TypedQuery<Announcement> query = entityManager.createQuery(jpql, Announcement.class); query.setParameter("now", new Date()); query.setParameter("departm

Hibernate interceptors

I am trying to implement method described here , but can't make it work. There are no errors during compilation, but the interceptor never fired. DAO: public class GeneralInvoicesDAO { @Autowired private SessionFactory sessionFactory; @Autowired private Interceptor entityInterceptor; @Transactional public void update(GeneralInvoice object) { Session session = SessionFactoryUtils.getSession(sessionFactory, entityInterceptor, null); session.saveOrUpdate(object); } } The intercepter class: public class NewEntityInterceptor extends EmptyInterceptor implements Interceptor { /** * */ private static final long serialVersionUID = 2914362528125673753L; @Override public Boolean isTransient(Object n) { Logger logger = Logger.getLogger(getClass().getName()); try { logger.warn("test"); Boolean result = Boolean.FALSE; BaseEntity entity = (BaseEntity) n; if (enti

JPA 2 + Criteria API - Defining a subquery

I try to convert a sql query to Criteria API without success so far. I can create two separate queries which return the values I need, but I don't know how to combine them in a single query. Here is the sql statement which works: select company.*, ticketcount.counter from company join (select company, COUNT(*) as counter from ticket where state<16 group by company) ticketcount on company.compid = ticketcount.company; This Criteria query returns the inner query results: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<intCompany> qTicket = cb.createQuery(intCompany.class); Root<Ticket> from = qTicket.from(Ticket.class); Path groupBy = from.get("company"); Predicate state = cb.notEqual(from.<State>get("state"), getStateById(16)); qTicket.select(cb.construct( intCompany.class, cb.count(from),from.<Company>get("company"))) .where(state).groupBy(groupBy); em.createQuery