jpa-2.0

Is this possible: JPA/Hibernate query with list property in result?

雨燕双飞 提交于 2019-11-28 21:30:26
In hibernate I want to run this JPQL / HQL query: select new org.test.userDTO( u.id, u.name, u.securityRoles) FROM User u WHERE u.name = :name userDTO class: public class UserDTO { private Integer id; private String name; private List<SecurityRole> securityRoles; public UserDTO(Integer id, String name, List<SecurityRole> securityRoles) { this.id = id; this.name = name; this.securityRoles = securityRoles; } ...getters and setters... } User Entity: @Entity public class User { @id private Integer id; private String name; @ManyToMany @JoinTable(name = "user_has_role", joinColumns = { @JoinColumn

@OneToOne(optional=false) and @JoinColumn(nullable=false) used together

故事扮演 提交于 2019-11-28 21:22:05
问题 I've bumped into this example in JPA 2.0 FR Specification, 11.1.37. OneToOne Annotation, page 403: @OneToOne(optional=false) @JoinColumn(name="CUSTREC_ID", unique=true, nullable=false, updatable=false) public CustomerRecord getCustomerRecord() { return customerRecord; } Is there any reason that I should put @OneToOne(optional=false) and at that same time put @JoinColumn(... nullable=false) ? Aren't these two declarations the same? Isn't one of them redundant? Are both of them used in DDL

Abstracting named queries in an abstract JPA DAO

女生的网名这么多〃 提交于 2019-11-28 20:50:49
I have an abstract DAO class which uses parameterized types E (Entity) and K (Primary Key). In every entity I have a @NamedQuery . I want to dynamically invoke this named query without knowing its exact name and parameter name. As an example, imagine the following entity City @Entity(name="CITY") @NamedQuery( name="findCityByname", query="FROM CITY c WHERE name = :CityName" ) public class City { // ... } and this CityDao public class CityDao extends AbstractDao<City, Long> { public CityDao() { super(City.class); } } How should I implement the findByName() method in AbstractDao so that I don't

Triggers versus JPA Event

坚强是说给别人听的谎言 提交于 2019-11-28 19:25:51
I'm doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider, MySql 5.1.x. The application runs on Tomcat 7.X. In my entities I have some date like last update date: @Column(name = "last_update_date", insertable = false, updatable = false) @Temporal(TemporalType.TIMESTAMP) private Date lastUpdateDate; For the moment I have a trigger that updates: CREATE TRIGGER upd_site BEFORE UPDATE ON site FOR EACH ROW SET NEW.last_update_date = CURRENT_TIMESTAMP(); It works fine, but I just notice that there is some callbacks methods in JPA http://www.objectdb.com/java/jpa

Why should I specify @Column( nullable = false )?

青春壹個敷衍的年華 提交于 2019-11-28 19:07:27
I have an entity annotated with @Entity . If I am responsible for creating the CREATE TABLE scripts why should I specify @Column( nullable = false ) when I can create a column in the database with the NOT NULL keywords? Is there any example that shows the benefits of using this property in a field? Craig Ringer Better error messages and error handling, especially if you also add the JSR303 @NotNull annotation . If you create the column as NOT NULL but don't tell JPA it's not null, JPA will assume that null values are OK. When you try to save an object with nulls, it'll proceed to send that to

JPA, Entity manager, select many columns and get result list custom objects

大憨熊 提交于 2019-11-28 17:58:21
How I can get list of custom objects, like results below query: SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id By example: return getEntityManager().createQuery("SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id").setParameter("id", id).getResultList(); I need a map with category id and number of products in category. Unfortunately, JPA doesn't provide a standard way to retrieve the results in a Map . However, building up your map manually by walking through the result list is

JPA: When to choose Multivalued Association vs. Element Collection Mapping

*爱你&永不变心* 提交于 2019-11-28 17:48:55
I would like to better understand the differences between (1) a traditional Multivalued Relationship/Association @Entity -> @OneToMany -> @Entity and (2) the JPA2 Collection of Embeddable (and basic) Types @Entity -> @ElementCollection -> @Embeddable I see the syntactical differences, but wonder whether there are also performance implications . Under the hood, the database implementation looks very similar. Intuitively, I would typically use the @ElementCollection for composition scenarios . But even that feels very similar like CascadeType=DELETE . Am I missing the essence here? Is one more

JPA2: Case-insensitive like matching anywhere

丶灬走出姿态 提交于 2019-11-28 17:45:52
I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There is defined Restrictions.ilike("column","keyword", MatchMode.ANYWHERE) which tests if the keyword matching the column anywhere and it is case-insensitive. Now, I am using JPA 2.0 with EclipseLink as driver so I have to use "Restrictions" build-in JPA 2.0. I found CriteriaBuilder and method like , I have also found out how to make it matching anywhere ( although it is aweful and manual ), but still I haven't figured out how to do it case-insensitive. There is my current aweful solution: CriteriaBuilder builder = em

JPA/Criteria API - Like & equal problem

只愿长相守 提交于 2019-11-28 16:58:27
I'm trying to use Criteria API in my new project: public List<Employee> findEmps(String name) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> emp = c.from(Employee.class); c.select(emp); c.distinct(emp); List<Predicate> criteria = new ArrayList<Predicate>(); if (name != null) { ParameterExpression<String> p = cb.parameter(String.class, "name"); criteria.add(cb.equal(emp.get("name"), p)); } /* ... */ if (criteria.size() == 0) { throw new RuntimeException("no criteria"); } else if (criteria.size() == 1) { c.where(criteria

Hibernate JPA, MySQL and TinyInt(1) for Boolean instead of bit or char

拈花ヽ惹草 提交于 2019-11-28 16:49:59
Here is my JPA2 / Hibernate definition: Code: @Column(nullable = false) private boolean enabled; In MySql this column is resolved to a bit(1) datatype - which does not work for me. For legacy issues I need to map the boolean to a tinyint not to a bit. But I do not see a possibility to change the default datatype. Is there any? Mike Q Try the NumericBooleanType . For some reason this doesn't have a declared short type name so you'd have to use: @Column(nullable = false) @Type(type = "org.hibernate.type.NumericBooleanType") private boolean enabled; This does map to an INTEGER type but it will