Enable editing the power settings.
Showing posts with label hibernate. Show all posts
Showing posts with label hibernate. Show all posts
Monday, April 20, 2020
Thursday, June 07, 2012
hibernate3:hbm2ddl - Could not determine type for: java.util.List
Trying to generate DB tables from Java classes, I got this error for this one-to-many mapping:
@ElementCollection(targetClass = DocArea.class)
@CollectionTable(name = "area_doc",
joinColumns = @JoinColumn(name = "id_doc"))
@Column(name = "id_area")
public List getAreas() {
return areas;
}
where Area is an enumeration.
It was a version incompatibility between the hibernate3 plugin and Hibernate itself. I found out you can override a plugin's dependencies in the configuration of the plugin itself thanks to Ivar's post.
@ElementCollection(targetClass = DocArea.class)
@CollectionTable(name = "area_doc",
joinColumns = @JoinColumn(name = "id_doc"))
@Column(name = "id_area")
public List
return areas;
}
where Area is an enumeration.
It was a version incompatibility between the hibernate3 plugin and Hibernate itself. I found out you can override a plugin's dependencies in the configuration of the plugin itself thanks to Ivar's post.
Monday, May 14, 2012
No Cascade Delete on a Collection Relationship
I had the following relationship in a UserAnswer, the answer(s) a user can give to a question in a survey:
I wanted to propagate all persistence operations to the collection except DELETE. I didn't want an Answer to be deleted when all UserAnswers were removed.
This is how it worked for me:
@ManyToMany(targetEntity=Answer.class, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinTable(name="userAnswer_answers",
joinColumns=@JoinColumn(name="userAnswer_fk"),
inverseJoinColumns=@JoinColumn(name="answer_fk"))
@Sort(type = SortType.COMPARATOR, comparator = AnswerPositionComparator.class)
public SortedSet
return selectedAnswers;
}
If you don't use CascadeType.ALL, Hibernate doesn't cascade the save of an entity's Collection properties when using the PERSIST annotation. You have to explicitly use the org.hibernate.annotations.CascadeType.SAVE_UPDATE annotation.
Source here.
Thursday, September 09, 2010
Spring Open View In Session Pattern For Portlets
I needed to implement the Open Session In View pattern for a portlet using Spring (3 with annotations) and Hibernate.
This pattern allows lazily loaded objects to have access to the Hibernate Session for the entire processing of the render request despite the original transaction (associated to the portlet request) already being completed.
I.e. If you have One To Many associations, this allows you to load the Many side of the association in the same thread of the request where you loaded the One side.
What I needed to do is:
Add a WebRequestHandlerInterceptorAdapter interceptor wrapping the stantard OpenSessionInViewInterceptor to applicationContext.xml:
then add it to the DefaultAnnotationHandlerMapping in yourPortlet-portlet.xml:
This pattern allows lazily loaded objects to have access to the Hibernate Session for the entire processing of the render request despite the original transaction (associated to the portlet request) already being completed.
I.e. If you have One To Many associations, this allows you to load the Many side of the association in the same thread of the request where you loaded the One side.
What I needed to do is:
Add a WebRequestHandlerInterceptorAdapter interceptor wrapping the stantard OpenSessionInViewInterceptor to applicationContext.xml:
<bean name="openSessionInViewInterceptor"
class="org.springframework.web.portlet.handler.WebRequestHandlerInterceptorAdapter">
<constructor-arg>
<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</constructor-arg>
</bean>
then add it to the DefaultAnnotationHandlerMapping in yourPortlet-portlet.xml:
<bean id="annotationMapper"
class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="10" />
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor"/>
</list>
</property>
</bean>
Labels:
annotations,
hibernate,
open session in view,
spring framework
Friday, April 16, 2010
Hibernate ManyToOne Association With Annotations
It took me a while to get rid of a:
ERROR org.hibernate.util.JDBCExceptionReporter -
Unknown column 'applicatio0_.domain_domainID' in 'field list'
on a simple many-to-one association.
I had an Application object with a Domain field where many Applications could have the same Domain (standard many-to-one association).
The annotations on the field were:
Class Domain started with:
Hibernate couldn't correctly map the domain ID until I added the JoinColumn annotation:
which in my opinion could be inferred from class Domain:
but it wasn't.
Always use JoinColumn.
ERROR org.hibernate.util.JDBCExceptionReporter -
Unknown column 'applicatio0_.domain_domainID' in 'field list'
on a simple many-to-one association.
I had an Application object with a Domain field where many Applications could have the same Domain (standard many-to-one association).
The annotations on the field were:
@ManyToOne(cascade = CascadeType.ALL)
public Domain getDomain() {
return domain;
}
Class Domain started with:
@Entity
@Table(name="domain")
public class Domain implements Serializable {
// DB name: domainID
private Long id;
private String name;
Hibernate couldn't correctly map the domain ID until I added the JoinColumn annotation:
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="domainID")
public Domain getDomain() {
return domain;
}
which in my opinion could be inferred from class Domain:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "domainID", unique = true,
updatable = false, nullable = false)
public Long getId() {
return this.id;
}
but it wasn't.
Always use JoinColumn.
Subscribe to:
Posts (Atom)