Tag Archives: jpa

JPA With Hibernate Error: [PersistenceUnit: JPA] Unable to build EntityManagerFactory

Questions: I have a problem with Java Persistence API and Hibernate. My situation of project is: My persistence.xml file is: <persistence xmlns=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd” version=”2.0″> <persistence-unit name=”JPA”> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.david.Libro</class> <class>com.david.Categoria</class> <properties> <property name=”hibernate.show_sql” value=”true” /> <property name=”javax.persistence.transactionType” value=”RESOURCE_LOCAL” /> <property name=”javax.persistence.jdbc.driver” value=”com.mysql.jdbc.Driver” /> <property name=”javax.persistence.jdbc.url” value=”jdbc:mysql://localhost/arquitecturaJava” /> <property name=”javax.persistence.jdbc.user” value=”root” /> <property name=”javax.persistence.jdbc.password” value=”root”… Read More »

JPA/Hibernate Native Queries do not recognize Parameters

Questions: I am using Hibernate/JPA to execute native PostGIS queries. The problem with these queries is that they need parameters that are not of the classical X = ‘value’ form. For example, the following lines crash String queryString = “select * from Cell c where ST_DWithin(c.shape, SetSRID(ST_GeomFromEWKT(‘POINT(:lon :lat)’),4326), 0.1)”; Query query = Cell.em().createNativeQuery(queryString, Cell.class); query.setParameter(“lon”,… Read More »

How to create and handle composite primary key in JPA

Questions: I want to have versions from the same data entry. In other words, I want to duplicate the entry with another version number. id – Version will be the primary key. How should the entity look like? How can I duplicate it with another version? id Version ColumnA 1 0 Some data 1 1… Read More »

setMaxResults for Spring-Data-JPA annotation?

Questions: I am trying to incorporate Spring-Data-JPA to my project. One thing confuses me is how to achieve setMaxResults(n) by annotation ? for example , my code: public interface UserRepository extends CrudRepository<User , Long> { @Query(value=”From User u where u.otherObj = ?1 “) public User findByOhterObj(OtherObj otherObj); } I only need to return one (and… Read More »

How to load lazy fetched items from Hibernate/JPA in my controller

Questions: I have a Person class: @Entity public class Person { @Id @GeneratedValue private Long id; @ManyToMany(fetch = FetchType.LAZY) private List<Role> roles; // etc } With a many-to-many relation that is lazy. In my controller I have @Controller @RequestMapping(“/person”) public class PersonController { @Autowired PersonRepository personRepository; @RequestMapping(“/get”) public @ResponseBody Person getPerson() { Person person =… Read More »

Drawing rectangle on a JPanel

Questions: I want to draw a recangle on a JPanel. Am able to draw with the following code. public class DrawingColor extends JFrame { public static void main(String[] args) { DrawingColor d = new DrawingColor(); } public DrawingColor() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(new MyComponent()); setSize(400,400); setVisible(true); } public class MyComponent extends JComponent { @Override public void paint(Graphics… Read More »

Rotate BufferedImage Inside JPanel

Questions: I am trying to rotate a BufferedImage and display it inside a JLabel (which is inside a JPanel). The current result produces a white square rotated 10 degrees against a black background, but the image is not present inside the square. I know myPicture is not blank, since myPicture itself displays properly inside the… Read More »

“detached entity passed to persist error” with JPA/EJB code

Questions: I am trying to run this basic JPA/EJB code: public static void main(String[] args){ UserBean user = new UserBean(); user.setId(1); user.setUserName(“name1”); user.setPassword(“passwd1”); em.persist(user); } I get this error: javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.JPA.Database Any ideas? I search on the internet and the reason I found was: This was caused by… Read More »

I found JPA, or alike, don't encourage DAO pattern

Questions: I found JPA, or alike, don’t encourage DAO pattern. I don’t know, but I feel like that, especially with server managed JTA managers. After adequate hands-on using DAO pattern, I started designing JPA based application around that pattern. But it doesn’t fit in, IMO. I tend to lose quite a features of JPA and… Read More »

JPA and Hibernate – Criteria vs. JPQL or HQL

Questions: What are the pros and cons of using Criteria or HQL? The Criteria API is a nice object-oriented way to express queries in Hibernate, but sometimes Criteria Queries are more difficult to understand/build than HQL. When do you use Criteria and when HQL? What do you prefer in which use cases? Or is it… Read More »