A
A
Andrey Kovalchuk2016-04-06 12:59:46
Hibernate
Andrey Kovalchuk, 2016-04-06 12:59:46

How to setup many-to-one dependency (hibernate, spring, jfs)?

Good day. Tell me how to set up a many-to-one connection correctly using hibernate?
Repository Link: Notebook
Intro: We have a Notebook application. The database is designed in such a way that all records are stored in the main Note table as the id of their components: person, phone, description, group. With all other tables, a One-to-one relationship is established and everything works well.
But with a group, the connection is many-to-one, and here the problems begin. The problem is: I don't know how to do it on the user side (JSF-page).
In the Test.xhtml file (just below), I call a method that pulls all records from the Group table, but there is one "but", I need them: 1. Displayed on the client side not as objects, but as group names; 2. So that when you click Save (see Test.xhtml), either only Id or a complete object is passed to the method.

Body Test.xhtml

<body>
<f:view>
    <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
    <h1>Create Note</h1>
    <h:form>
        <h:panelGrid columns="2">

            <h:outputText value="Фамилия:"/>
            <h:inputText value="#{human.family}"/>
            <h:outputText value="Имя:"/>
            <h:inputText value="#{human.name}"/>
            <h:outputText value="Отчество:"/>
            <h:inputText value="#{human.secondName}"/>
            <h:outputText value="Дата:"/>
            <h:inputText value="#{human.birthday}">
                <f:convertDateTime type="date" pattern="dd-MM-yyyy"/>
            </h:inputText>
            <h:outputText value="Телефон:"/>
            <h:inputText value="#{telephone.telephone}"/>
            <h:outputText value="Группа:"/>
            <!--<ui:repeat value="${groupDAOImpl.group}" var="h">
                <h:selectOneMenu value="#{group.group}"  title="group">
                    <f:selectItems  value="${h.group}" var="h"/>
                </h:selectOneMenu>
            </ui:repeat>-->
            <h:outputText value="Описание:"/>
            <h:inputText value="#{description.text}"/>

        </h:panelGrid>

        <h:commandButton action="#{notebookFacade.createNote(human,telephone,description)}" value="Save" />
        <h:commandButton action="descriptionList" value="Cancel"/>
        <br/>
    </h:form>
</f:view>
</body>

The commented block is our goal. What should happen as a result: an entry with the id-shnik of the group selected by the user should be entered into the Note database (main database) (now it is entered, but without a group).
Mapping
<hibernate-mapping>

    <class name="Notebook.Enities.Note" table="note" schema="notebase">
        <id name="id" type="java.lang.Integer">
            <column name="Id" not-null="true"/>
            <generator class="identity"/>
        </id>
        <one-to-one class="Notebook.Enities.Telephone" name="telephoneId"  cascade="save-update"/>
        <one-to-one class="Notebook.Enities.Human" name="humanId" cascade="save-update"/>
        <!--<many-to-one class="Notebook.Enities.Group" name="group" column="Group" cascade="all" not-null="true"/>-->
    </class>
</hibernate-mapping>

Our target is the commented out line.
GroupDAOImpl.java
@Component
public class GroupDAOImpl implements groupDAO {
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    @Override
    public List<Group> getGroup(){
        return sessionFactory.getCurrentSession().createCriteria(Group.class).list();
    }
}

If you need any other materials for the solution, write in the comments.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ildar Gafarov, 2016-04-07
@mrkovalchuk

I suspect that you need to describe the converter so that jsf can pull out the desired Group object from id. <ui:repeat value="${groupDAOImpl.group}" var="h">in the commented piece is not needed, it is enough

<h:selectOneMenu value="#{group.group}"  title="group">
    <f:selectItems  value="${h.group}" var="h"/>
</h:selectOneMenu>
plus converter description

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question