ChoosePreference réparé !

De gros problèmes avec HSQLDB, et la persistance en général (principalement du aux IdClass qu'on a ajouté).
This commit is contained in:
2008-01-14 01:29:04 +00:00
parent 0d84a0d77f
commit b19e6e154a
8 changed files with 91 additions and 52 deletions

View File

@@ -3,6 +3,7 @@
<jta-data-source>java:/DefaultDS</jta-data-source> <jta-data-source>java:/DefaultDS</jta-data-source>
<properties> <properties>
<property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.batch_size" value="0"/>
</properties> </properties>
</persistence-unit> </persistence-unit>
</persistence> </persistence>

View File

@@ -67,13 +67,13 @@ public class ArticleManagerBean implements IArticleManager, Serializable {
return (List<User>) article.getReferees(); return (List<User>) article.getReferees();
} }
public Preference getArticlePreferenceForUser(Article article, public Preference getArticlePreferenceForUser(Integer article_id,
String pcMemberLogin) { String pcMemberLogin) {
Query query = em.createQuery("SELECT p FROM Preference p WHERE p.article.id = :articleid AND p.pcMember.id = :pcmemberlogin"); PreferencePK preferenceid = new PreferencePK();
query.setParameter("articleid",article.getId()); preferenceid.article = em.find(Article.class, article_id);
query.setParameter("pcmemberlogin", pcMemberLogin); preferenceid.pcMember = em.find(User.class, pcMemberLogin);
try { try {
return (Preference) query.getSingleResult(); return (Preference) em.find(Preference.class, preferenceid);
} catch (NoResultException e) { } catch (NoResultException e) {
return null; return null;
} }
@@ -98,11 +98,20 @@ public class ArticleManagerBean implements IArticleManager, Serializable {
preference.setPcMember(pcMember); preference.setPcMember(pcMember);
preference.setPreference(preferenceValue); preference.setPreference(preferenceValue);
em.persist(preference); em.persist(preference);
em.flush();
} }
public void addOrUpdatePreference(Preference preference){ public void updateArticlePreference(Integer article_id, String userLogin, String preferenceValue) {
if(existsPreference(preference)){ Preference preference = getArticlePreferenceForUser(article_id, userLogin);
updatePreference(preference); preference.setPreference(preferenceValue);
em.merge(preference);
}
public void addOrUpdatePreference(Integer article_id, String userLogin, String preferenceValue){
Preference preference = getArticlePreferenceForUser(article_id, userLogin);
if(preference != null){
preference.setPreference(preferenceValue);
em.merge(preference);
} else { } else {
addArticlePreference(preference); addArticlePreference(preference);
} }

View File

@@ -15,10 +15,10 @@ public interface IArticleManager {
public List<?> getArticlePCMembers(Article article); public List<?> getArticlePCMembers(Article article);
public List<?> getArticleReferees(Article article); public List<?> getArticleReferees(Article article);
public Preference getArticlePreferenceForUser(Article article, String pcMemberLogin); public Preference getArticlePreferenceForUser(Integer article_id, String pcMemberLogin);
public void setArticlePreferences(Article article, List<Preference> preferences); public void setArticlePreferences(Article article, List<Preference> preferences);
public void addArticlePreference(Integer article_id, String userLogin, String preferenceValue); public void addArticlePreference(Integer article_id, String userLogin, String preferenceValue);
public void addOrUpdatePreference(Preference preference); public void addOrUpdatePreference(Integer article_id, String userLogin, String preferenceValue);
public boolean existsArticle(Article article); public boolean existsArticle(Article article);
public boolean existsPreference(Preference preference); public boolean existsPreference(Preference preference);

View File

@@ -80,8 +80,8 @@ public class Preference implements Serializable {
public PreferencePK getId(){ public PreferencePK getId(){
PreferencePK id = new PreferencePK(); PreferencePK id = new PreferencePK();
id.setArticle(this.article); id.article = this.article;
id.setPcMember(this.pcMember); id.pcMember = this.pcMember;
return id; return id;
} }
} }

View File

@@ -3,8 +3,19 @@ package org.yacos.core.article;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.yacos.core.users.User; import org.yacos.core.users.User;
/**
* PreferencePK
* Primary key for preferences
*
* @author christiancorsano
*
*/
public class PreferencePK implements Serializable{ public class PreferencePK implements Serializable{
/** /**
@@ -12,23 +23,33 @@ public class PreferencePK implements Serializable{
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private User pcMember; /**
* Public key, following these recomandations : http://www.jpox.org/docs/1_2/primary_key.html
*/
@Id
@ManyToOne(targetEntity=User.class)
@JoinColumn(name="pcmember_id",nullable=false)
public User pcMember;
/**
* Public key, following these recomandations : http://www.jpox.org/docs/1_2/primary_key.html
*/
@Id
@ManyToOne(targetEntity=Article.class)
@JoinColumn(name="article_id",nullable=false)
public Article article;
private Article article; // @Override
// public boolean equals(Object obj) {
public User getPcMember() { // if (! (obj instanceof PreferencePK) ) {
return pcMember; // return false;
} // }
// PreferencePK other = (PreferencePK) obj;
public void setPcMember(User pcMember) { // return this.pcMember.getLogin().equals(other.pcMember.getLogin()) &&
this.pcMember = pcMember; // this.article.getId() == other.article.getId();
} // }
//
public Article getArticle() { // @Override
return article; // public int hashCode() {
} // return this.pcMember.getLogin().hashCode() ^ this.article.getId();
// }
public void setArticle(Article article) {
this.article = article;
}
} }

View File

@@ -12,30 +12,33 @@ public class RolePK implements Serializable{
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private RoleType type; /**
private User user; * Public key, as described here : http://www.jpox.org/docs/1_2/primary_key.html
private Conference conference; */
public RoleType type;
/**
* Public key, as described here : http://www.jpox.org/docs/1_2/primary_key.html
*/
public User user;
/**
* Public key, as described here : http://www.jpox.org/docs/1_2/primary_key.html
*/
public Conference conference;
@Override
public boolean equals(Object obj) {
public RoleType getType() { if (!(obj instanceof RolePK)) {
return type; return false;
} }
public void setType(RoleType type) { RolePK otherPK = (RolePK) obj;
this.type = type; return type.equals(otherPK.type)&&
} user.getLogin().equals(otherPK.user.getLogin())&&
public User getUser() { conference.getId() == otherPK.conference.getId();
return user;
}
public void setUser(User user) {
this.user = user;
}
public Conference getConference() {
return conference;
}
public void setConference(Conference conference) {
this.conference = conference;
} }
@Override
public int hashCode() {
return type.hashCode() ^ user.hashCode() ^ conference.hashCode();
}
} }

View File

@@ -4,6 +4,7 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@@ -29,6 +30,7 @@ public class User implements Serializable{
* Is used to identify the user, should be unique and can't be modified * Is used to identify the user, should be unique and can't be modified
*/ */
@Id @Id
@Column(name = "login")
private String login; private String login;
/** /**
* Password : used for authentication purpose only, is only writable * Password : used for authentication purpose only, is only writable
@@ -72,6 +74,7 @@ public class User implements Serializable{
} }
@Id @Id
@Column(name = "login")
public String getLogin() { public String getLogin() {
return login; return login;
} }
@@ -144,6 +147,7 @@ public class User implements Serializable{
/** /**
* @param login the login to set * @param login the login to set
*/ */
@Column(name = "login")
public void setLogin(String login) { public void setLogin(String login) {
this.login = login; this.login = login;
} }

View File

@@ -38,6 +38,7 @@ public class UserManagerBean implements IUserManager{
if (!this.exists(login)){ if (!this.exists(login)){
User user = new User(login, password, firstName, lastName, organization, email); User user = new User(login, password, firstName, lastName, organization, email);
em.persist(user); em.persist(user);
em.flush();
return user; return user;
} }
else{ throw new PKAlreadyUsedException(); } else{ throw new PKAlreadyUsedException(); }