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>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.batch_size" value="0"/>
</properties>
</persistence-unit>
</persistence>

View File

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

View File

@@ -15,10 +15,10 @@ public interface IArticleManager {
public List<?> getArticlePCMembers(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 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 existsPreference(Preference preference);

View File

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

View File

@@ -3,8 +3,19 @@ package org.yacos.core.article;
import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.yacos.core.users.User;
/**
* PreferencePK
* Primary key for preferences
*
* @author christiancorsano
*
*/
public class PreferencePK implements Serializable{
/**
@@ -12,23 +23,33 @@ public class PreferencePK implements Serializable{
*/
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;
public User getPcMember() {
return pcMember;
}
public void setPcMember(User pcMember) {
this.pcMember = pcMember;
}
public Article getArticle() {
return article;
}
public void setArticle(Article article) {
this.article = article;
}
// @Override
// public boolean equals(Object obj) {
// if (! (obj instanceof PreferencePK) ) {
// return false;
// }
// PreferencePK other = (PreferencePK) obj;
// return this.pcMember.getLogin().equals(other.pcMember.getLogin()) &&
// this.article.getId() == other.article.getId();
// }
//
// @Override
// public int hashCode() {
// return this.pcMember.getLogin().hashCode() ^ this.article.getId();
// }
}

View File

@@ -12,30 +12,33 @@ public class RolePK implements Serializable{
*/
private static final long serialVersionUID = 1L;
private RoleType type;
private User user;
private Conference conference;
/**
* Public key, as described here : http://www.jpox.org/docs/1_2/primary_key.html
*/
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;
public RoleType getType() {
return type;
}
public void setType(RoleType type) {
this.type = type;
}
public User getUser() {
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 boolean equals(Object obj) {
if (!(obj instanceof RolePK)) {
return false;
}
RolePK otherPK = (RolePK) obj;
return type.equals(otherPK.type)&&
user.getLogin().equals(otherPK.user.getLogin())&&
conference.getId() == otherPK.conference.getId();
}
@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.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
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
*/
@Id
@Column(name = "login")
private String login;
/**
* Password : used for authentication purpose only, is only writable
@@ -72,6 +74,7 @@ public class User implements Serializable{
}
@Id
@Column(name = "login")
public String getLogin() {
return login;
}
@@ -144,6 +147,7 @@ public class User implements Serializable{
/**
* @param login the login to set
*/
@Column(name = "login")
public void setLogin(String login) {
this.login = login;
}

View File

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