0

I'm newest in JSF technology, I need to use JSF and Hibernate. But I have got a question that connected with adequacy of code. How correctly use Hibernate mapping with jsf? Would be good to use hibernate annotations with @ManagedBean annotation together? I have got my Field class that needed to be inserted to DB. And I puted here @ManagedBean and @SessionScoped annotations with Hibernate annotations.

@ManagedBean(name = "field")
@SessionScoped
@Entity
@Table(name = "field", catalog = "infostroy", uniqueConstraints = { @UniqueConstraint(columnNames = "label") })
public class Field {
    @Id
    @Column(name = "label", unique = true, nullable = false, length = 70)
    private String label;
    @Column(name = "type", nullable = false, length = 70)
    private Type type;
    @Column(name = "required", nullable = false)
    private boolean required;
    @Column(name = "isActive", nullable = false)
    private boolean isActive;

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

}

And in my FieldManageBean class I'm doing something like this

    @ManagedBean(name = "fieldManageBean")
@RequestScoped
public class FieldManageBean {
    @ManagedProperty("#{field}")
    private Field field;
    @ManagedProperty("#{fieldService}")
    private FieldService fieldService;

    public FieldService getFieldService() {
        return fieldService;
    }

    public void setFieldService(FieldService fieldService) {
        this.fieldService = fieldService;
    }

    public List<String> getFieldTypes() {
        return Type.getStringListOfEnums();
    }

}

Is it a good practicies of using Hibernate with JSF, or not? Or I need to map my entities using .xml? Could you explain me or give some examples of it. Thanx

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Serg Shapoval
  • 707
  • 1
  • 11
  • 39
  • Do not use entity classes directly in the view – Jens Apr 13 '17 at 11:34
  • @Jens: why not? – Kukeltje Apr 13 '17 at 11:37
  • @Kukeltje because there a re a lot of database relevant metadata in these objects. – Jens Apr 13 '17 at 11:40
  • @Kukeltje, also that's what code decoupling is all about. Hibernate entities represent the model whereas the view/backing bean represents the view/controller part an MVC application/pattern. Needless to say, this has many benefits. – dsp_user Apr 13 '17 at 11:43
  • @Jens, I use entity classes in FieldManageBean class. Is it ok or not? – Serg Shapoval Apr 13 '17 at 11:51
  • 1
    @SergShapoval in my understanding of good software architecture: No – Jens Apr 13 '17 at 11:53
  • @Jens, but how to use them correctly? Or only create them like an objects in manage bean classes ? – Serg Shapoval Apr 13 '17 at 11:55
  • read about 3 tier architecture (DAO, Service, MVC) and so on – Jens Apr 13 '17 at 11:57
  • @Jens, I know this architecture. I've got an expirience in it. But I dont know best practicies in JSF 3 tier arch – Serg Shapoval Apr 13 '17 at 12:06
  • http://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao – Kukeltje Apr 13 '17 at 14:10
  • In the duplicate entities **are** used in the managedbean. Nothing wrong with that, Oracle does it to in the PetStore successor ([cargo-tracker[(https://cargotracker.java.net/)). What is not good is to make an entity a managedbean itself, create a field for it in the manageBean (this is effectively a better duplicate: http://stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean). BeanValidation in btw JSF works by reading annotations on entities for validation, so duplicating that in additional 'dto' or 'dvo' or whatever objects would be not smart – Kukeltje Apr 13 '17 at 19:10

0 Answers0