English 中文(简体)
JDBC
原标题:JDBC many-to-one mapping / null pointer exception error
  • 时间:2011-11-22 18:09:52
  •  标签:
  • java
  • jdbc

因此,我一直在为该项目工作,我正在利用亚行+共同基金。

目前,我有3个农业、用户、群体和用户小组。

我的问题是,你如何在三类人、用户、群体和用户群体之间建立联系?

在我的数据库中,用户小组是一个联系实体,拥有IDUSer和IDG作为主要的复合钥匙。

用户与用户小组之间的关系是1到许多人。 用户小组和用户小组之间的关系也为1至5,用户小组与用户和用户群体有着许多至1的关系。

如果我想在用户与用户小组、小组和用户小组之间建立一种关系,这是否正确?

public class UsuariousGrupos {

Integer id_usuario;
Integer id_grupo;
Grupos  groups;
Usuarious users;

public UsuariousGrupos() {
}

public UsuariousGrupos(Integer id_usuario, Integer id_grupo) {
    this.id_usuario = id_usuario;
    this.id_grupo = id_grupo;
}

public Integer getId_grupo() {
    return id_grupo;
}

public void setId_grupo(Integer id_grupo) {
    this.id_grupo = id_grupo;
}

public Integer getId_usuario() {
    return id_usuario;
}

public void setId_usuario(Integer id_usuario) {
    this.id_usuario = id_usuario;
}

public Grupos getGroups() {
    return groups;
}

public void setGroups(Grupos groups) {
    this.groups = groups;
}

public Usuarious getUsers() {
    return users;
}

public void setUsers(Usuarious users) {
    this.users = users;
}

此外,我测试了我的农业部的联系,他们正在做罚款。 我能够从我的用户数据库表和小组数据库表上显示姓名/说明。 然而,由于我的用户小组桌上只储存了身份证,我只能展示身份证。 我想显示与该识别有关的用户/集团的名称/描述。 然而,当我做以下工作时:

 List<UsuariousGrupos> ugdList = ugd.list() ;
    System.out.println("List of users successfully queried: " + ugdList);
    System.out.println("Thus, amount of usergroups in database is: " + ugdList.size());
    for (int i = 0; i < ugdList.size(); i++) {
        System.out.println("Groups in database are: " + ugdList.get(i).getUsers().getNome());
        System.out.println("Users in database are: " + ugdList.get(i).getGroups().getDescricao());
    }

我在read子“主食”的lang子里有一个例外。 Null PointerException. 显然,我无法从我的用户组名单查阅用户和团体的标语: / 这使我想,我缺乏这几类人之间的某种关系。

无效例外情形按以下方式发生:

System.out.println("Groups in database are: " + ugdList.get(i).getUsers().getNome());
System.out.println("Users in database are: " + ugdList.get(i).getGroups().getDescricao()

表面上看,有色人种(i).getUsers()和ugdList.get(i)。

这方面的任何想法?

问题回答

您需要绘制每个用户的名称,并超载用户类别的方法,如:

public String toString()
{
return String.format(""%s %s", this.name, this.lastName);
}

By the way, ugdList.get(i).getUsers().getNome()); seems wrong to me But using the toString method applied earlier, you can do this :

for (User user : ugdList.get(i).getUsers())
{
printf(user);
}

BUT you should test ugdList.get(i).getUsers() by steps before : check that get(i).getUsers != null then run the loop.

希望这一帮助!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签