0

我在数据库中有一个表用户和专业化

用户:

CREATE  TABLE IF NOT EXISTS `ePrzychodnia`.`users` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `firstName` VARCHAR(45) NOT NULL ,
  `lastName` VARCHAR(45) NOT NULL ,
  `specialization_id` INT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `personalId_UNIQUE` (`personalId` ASC) ,
  INDEX `fk_users_specializations1_idx` (`specialization_id` ASC) ,
  CONSTRAINT `fk_users_specializations1`
    FOREIGN KEY (`specialization_id` )
    REFERENCES `ePrzychodnia`.`specializations` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

专长:

CREATE  TABLE IF NOT EXISTS `ePrzychodnia`.`specializations` (
  `id` INT NOT NULL ,
  `name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `name_UNIQUE` (`name` ASC) )
ENGINE = InnoDB;

实体类:

用户:

public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "firstName")
    private String firstName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "lastName")
    private String lastName;
    @Basic(optional = false)
    @JoinColumn(name = "specialization_id", referencedColumnName = "id")
    @ManyToOne
    private Specialization specializationId;

专业化:

public class Specialization implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "specializationId")
    private Collection<User> userCollection;

我尝试在 JSF 中为一个选择菜单创建转换器:

选择一个菜单:

                    <h:outputLabel for="specialization" value="#{msg.specialization}"/>
                    <p:selectOneMenu id="specialization" value="#{userMB.user.specializationId}" effect="fade" style="width:200px" converter="specializationConverter">
                        <f:selectItems value="#{specializationMB.allSpecialization}" var="specialization" itemValue="#{specialization}" itemLabel="#{specialization.name}"/>
                    </p:selectOneMenu>
                    <p:message for="specialization"/>

和转换器:

  public class SpecializationConverter implements Converter{

        private SpecializationDao specializationDao = new SpecializationDao();

        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Integer id = Integer.parseInt(value);
            return specializationDao.find(id);
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            return ((Specialization) value).getId().toString();
        }

}

但是转换器不起作用;/我有一个错误:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at pl.Project.dao.SpecializationDao.find(SpecializationDao.java:36)
    at pl.Project.converter.SpecializationConverter.getAsObject(SpecializationConverter.java:29)

专精道:

@Stateless
public class SpecializationDao implements SpecializationDaoLocal {

    @PersistenceContext
    private EntityManager em;
    private Specialization specialization;

    public SpecializationDao() {
    }

    public SpecializationDao(Specialization specialization) {
        this.specialization = specialization;
    }

    @Override
    public Specialization find(int specializationId) {
        return em.find(Specialization.class, specializationId);
    }

    @Override
    public List<Specialization> findAllSpecialization() {
        Query q = em.createNamedQuery("Specialization.findAll");
        List<Specialization> specializations = q.getResultList();
        return specializations;
    }
}
4

0 回答 0