【问题标题】:List ManyToMany related objects to the current user session列出当前用户会话的 ManyToMany 相关对象
【发布时间】:2014-03-07 06:33:36
【问题描述】:

我有一个按以下方式建模的现有数据库:

用户 - Id(PK)、登录名、密码

语言环境 - Id(PK)、描述、方向等...

users_locales - user_id(K), local_id(K)

实体类如下

用户

@Entity
@Table(name="users")
public class User {

@Id
@GeneratedValue
private Integer id;

private String login;

private String password;


@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_locales", joinColumns = {@JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "local_id") })
private List<Local> userLocales = new ArrayList<Local>();

本地

@Entity  
@Table(name="locales")
public class Local {  

@Id  
@Column(name="id")
@GeneratedValue  
private Integer id;  
@Column(name="descripcion")
private String descripcion;  
@Column(name="direccion")
private String direccion;  


@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_locales", joinColumns = {@JoinColumn(name = "local_id") }, inverseJoinColumns = { @JoinColumn(name = "user_id") })
private List<User> localesUser = new ArrayList<User>();

我需要在显示用户登录的“本地”合作伙伴列表的 jsp 页面中使用选项选择,但我无法正确部署它。

这还没有完全开发,我做错了一件或多件事情,因为我无法完全理解休眠和 Spring 安全性的工作原理。

需要在“UserService”中创建一个生成列表的服务,这就是我需要的帮助。

我添加了一些我认为此任务可能需要的代码 sn-ps。

用户服务

public interface UserService {

public User getUser(String login);

}

localServiceImpl

 @Transactional
 public List<Local> listLocal() {

 return localDAO.listLocal();
 }

 //This just show a list of all Local objects in database

localDAOImpl

@Repository
public class LocalDAOImpl implements LocalDAO {
@Autowired
private SessionFactory sessionFactory;

public List<Local> listLocal() {
return sessionFactory.getCurrentSession().createQuery("from Local")
.list();
}

}

控制器

@RequestMapping(value="/index", method=RequestMethod.GET)
public ModelAndView indexPage( Map<String, Object> map ) {
    map.put("localList", localService.listLocal());
    return new ModelAndView("home");
}

//Now its pointing to localService and this list all objets in database

home.jsp 文件

<select label="Locales" array="Locales" name="Locales">
    <c:forEach items="${localList}" var="local">
        <option value="${local.localidad}">
            <c:out value="${local.descripcion}"/>
        </option>
    </c:forEach>
</select>

如何列出与当前用户会话相关的“语言环境”?对可能的拼写或语法错误深表歉意。

【问题讨论】:

  • 我有点迷茫,你面临的具体问题是什么?
  • 感谢您的关注。 JSP 只显示表区域设置中的所有实例,而不是与登录用户相关的“区域设置”。
  • 那么请告诉我们localDAO.listLocal()的实现。
  • 哎呀,我的错。我已经编辑了帖子。

标签: java spring hibernate spring-security many-to-many


【解决方案1】:

此时您正在选择整个表格。您需要修改localDAO.listLocal() 并将登录名作为参数传递,然后修改您的查询并使用该参数来限制输出记录:

public List<Local> listLocal(String userLogin) {
    Query query = sessionFactory.getCurrentSession()
          .createQuery("select u.userLocales from User u where u.login = ?");
    query.setParameter(0, userLogin);
    return query.list();
}

Hibernate、Spring 或您使用的任何框架都不会自动限制数据。您需要自己限制当前用户的检索数据。

【讨论】:

  • 感谢您的帮助,但我有一些疑问...如何在 spring security 中获取当前用户作为参数?哪个是正确的地方?控制器还是 DAO?
  • How 完全取决于您的安全框架。 the best place 是控制器,因为用户的信息存储在会话中,可在 Web 层访问。
  • 我正在使用 final String currentUser = principal.getName(); 获取当前用户并将其传递给您发布的函数 listLocal 但我收到此错误:org.springframework.web.util.NestedServletException: Request processing失败的;嵌套异常是 org.hibernate.QueryParameterException:位置超出了声明的序数参数的数量。请记住,序数参数是基于 1 的!位置:1
  • 嗯,是的,它现在正在工作!!!与此处显示的内容不同的错误,添加回 query.setParameter(0, userlogin),正确的值为零。这是我的问题的正确答案。真的非常感谢! ;)
猜你喜欢
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 2020-12-07
相关资源
最近更新 更多