IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

[Annotation]Configuration avec Spring+composite key


Sujet :

Hibernate Java

  1. #1
    Nouveau membre du Club
    Inscrit en
    Mai 2008
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 39
    Points : 25
    Points
    25
    Par défaut [Annotation]Configuration avec Spring+composite key
    Bonjour,
    J'utilise comme Spring 2.0.6et comme hiebernate 3.2.6.ga.
    Je voudrais utiliser les annotations pour configurer l'accés au donné via Hibernate.
    Pourcela j'ai utilisé un fichier Application-hibernate-context.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
        <bean id="dataSource"
              class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName">
                <value>org.postgresql.Driver</value>
            </property>
            <property name="url">
                <value>jdbc:postgresql://localhost:5432/kmji18n</value>
            </property>
            <property name="username">
                <value>postgres</value>
            </property>
            <property name="password">
                <value>postgres</value>
            </property>
        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>                           <value>kmji18n.implementation.element.Label</value>
                </list>
            </property>
        </bean>
        <!--Hibernate-->
        <bean id="hibernateTemplate"
              class="org.springframework.orm.hibernate3.HibernateTemplate">
            <constructor-arg ref="sessionFactory"/>
        </bean>
    </beans>
    et dans mon classe Lavel:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
     
    @Entity
    @Table(name = "I18_LABEL")
     
    @NamedQueries(
            {
            @NamedQuery(
                    name = "label.findByLocale",
                    query = "from Label label where upper(label.labelPK.locale)=upper(?)"
            ),
            @NamedQuery(
                    name = "label.findAllCriteria",
                    query = "  from Label label where upper(label.labelPK.locale)=upper(?) and upper(label.labelPK.key)=upper(?) and\n" +
                            "        upper(label.value)=upper(?)"
            ),
            @NamedQuery(
                    name = "label.findAllKeys",
                    query = "select label.labelPK.key from Label label"
            )
                    ,
            @NamedQuery(
                    name = "label.findKeysByLocale",
                    query = "   delete from Label label where upper(label.labelPK.locale)=upper(?)"
            )
                    ,
            @NamedQuery(
                    name = "label.deleteByLocale",
                    query = "delete from Label label where upper(label.labelPK.locale)=upper(?)"
            )
                    ,
            @NamedQuery(
                    name = "label.deleteByKey",
                    query = "delete from Label label where upper(label.labelPK.key) like upper(?)"
            )
                    ,
            @NamedQuery(
                    name = "label.deleteAll",
                    query = "delete from Label label"
            )
                    }
    )
    public class Label implements ILabel {
        @Id
        private LabelPK labelPK = new LabelPK();
        @SuppressWarnings("unused")
        @Column(name = "I18_KEY", nullable = false, updatable = false, insertable = false)
        private String key;
        @SuppressWarnings("unused")
        @Column(name = "I18_LOCALE", nullable = false, updatable = false, insertable = false)
        private String locale;
        @Column(name = "I18_VALUE")
        private String value;
     
        public String getKey() {
            return labelPK.getKey();
        }
     
     
        public void setKey(String _key) {
            this.labelPK.setKey(_key);
            this.key = _key;
        }
     
        public String getLocale() {
            return this.labelPK.getLocale();
        }
     
        public void setLocale(String _locale) {
            this.labelPK.setLocale(_locale);
     
            this.locale = _locale;
        }
     
        public Label() {
            super();
        }
     
       public Label(LabelPK _labelPK, String _value) {
            this();
            this.labelPK = _labelPK;
            this.key = _labelPK.getKey();
            this.locale = _labelPK.getLocale();
            this.value = _value;
        }
     
        public void setPrimaryKey(LabelPK _labelPK) {
            this.labelPK = _labelPK;
        }
     
       public LabelPK getPrimaryKey() {
            return this.labelPK;
        }
     
        public LabelPK getLabelPK() {
            return this.labelPK;
        }
     
        public void setLabelPK(LabelPK _labelPK) {
            this.labelPK = _labelPK;
        }
     
        public String getValue() {
            return value;
        }
     
     
        public void setValue(String _value) {
            this.value = _value;
        }
     
     
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
     
            Label label = (Label) o;
     
            if (!this.getPrimaryKey().equals(label.getPrimaryKey())) {
                return false;
            }
            if (!value.equals(label.value)) {
                return false;
            }
            return true;
        }
     
        public int hashCode() {
            int result;
            result = this.getPrimaryKey().hashCode();
            result = 31 * result + value.hashCode();
            return result;
        }
     
     
    }
    Mais ça parait que la classe n'est pas persisté il me signale null pointer exception lorsque j'ai voulu créer un nouveau élément.

    y'a t' il quelqu'un qui a rencontré ce genre de problème .

    beaucoup d'avance

  2. #2
    Nouveau membre du Club
    Inscrit en
    Mai 2008
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 39
    Points : 25
    Points
    25
    Par défaut
    Bonjour,
    Le problème était ,enfin , résolu, c'etait en fait un mauvais import .Alors pour l'annotation @Entity il suffisait d'importer javax.persistence.Entity au lieu de org.hibernate.annotations.Entity et tous est réglé.
    Voila enfin ma version finale de code qui marche trés bien:
    La classe à persister:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
     
    import org.hibernate.annotations.NamedQueries;
    import org.hibernate.annotations.NamedQuery;
    import javax.persistence.Column;
    import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
    import javax.persistence.Table;
     
    @Entity
    @Table(name = "I18_LABEL")
    @NamedQueries(
            {
            @NamedQuery(
                    name = "label.findByLocale",
                    query = "from Label label where upper(label.labelPK.locale)=upper(?)"
            ),
            @NamedQuery(
                    name = "label.findAllCriteria",
                    query = "  from Label label where upper(label.labelPK.locale)=upper(?) and upper(label.labelPK.key)=upper(?) and\n" +
                            "        upper(label.value)=upper(?)"
            ),
            @NamedQuery(
                    name = "label.findAllKeys",
                    query = "select label.labelPK.key from Label label"
            )
                    ,
            @NamedQuery(
                    name = "label.findKeysByLocale",
                    query = "   delete from Label label where upper(label.labelPK.locale)=upper(?)"
            )
                    ,
            @NamedQuery(
                    name = "label.deleteByLocale",
                    query = "delete from Label label where upper(label.labelPK.locale)=upper(?)"
            )
                    ,
            @NamedQuery(
                    name = "label.deleteByKey",
                    query = "delete from Label label where upper(label.labelPK.key) like upper(?)"
            )
                    ,
            @NamedQuery(
                    name = "label.deleteAll",
                    query = "delete from Label label"
            )
                    }
    )
    public class Label implements ILabel {
        @EmbeddedId
        private LabelPK labelPK = new LabelPK();
        @Column(name = "I18_VALUE")
        private String value;
     
        /**
         * Constructor par défaut  de la classe Label
         */
        public Label() {
            super();
        }
     
        public Label(LabelPK _labelPK, String _value) {
            this();
            this.labelPK = _labelPK;
            this.value = _value;
        }
     
        public LabelPK getLabelPK() {
            return this.labelPK;
        }
     
           public void setLabelPK(LabelPK _labelPK) {
            this.labelPK = _labelPK;
        }
     
         public String getValue() {
            return value;
        }
     
        public void setValue(String _value) {
            this.value = _value;
        }
     
           public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
     
            Label label = (Label) o;
     
            if (!this.getPrimaryKey().equals(label.getPrimaryKey())) {
                return false;
            }
            if (!value.equals(label.value)) {
                return false;
            }
            return true;
        }
     
           public int hashCode() {
            int result;
            result = this.getPrimaryKey().hashCode();
            result = 31 * result + value.hashCode();
            return result;
        }
     
     
    }
    La classe de la clé primaire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    import javax.persistence.Column;
    import javax.persistence.Embeddable;
    import java.io.Serializable;
     
    @Embeddable
    public class LabelPK implements Serializable {
        private String locale;
        private String key;
     
        public LabelPK(String key, String locale) {
            this.key = key;
            this.locale = locale;
        }
     
        @Column(name = "I18_KEY", nullable = false)
        public String getKey() {
            return key;
        }
     
        public void setKey(String _key) {
            this.key = _key;
        }
     
        @Column(name = "I18_LOCALE", nullable = false)
        public String getLocale() {
            return locale;
        }
        public void setLocale(String _locale) {
            this.locale = _locale;
        }
    }
    Et enfin le fichier de configuration:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
        <bean id="dataSource"
              class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName">
                <value>org.postgresql.Driver</value>
            </property>
            <property name="url">
                <value>jdbc:postgresql://localhost:5432/kmji18n</value>
            </property>
            <property name="username">
                <value>postgres</value>
            </property>
            <property name="password">
                <value>postgres</value>
            </property>
        </bean>
     
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                    <value>kmji18n.implementation.element.Label</value>
                </list>
            </property>
        </bean>
        <!--Hibernate-->
        <bean id="hibernateTemplate"
              class="org.springframework.orm.hibernate3.HibernateTemplate">
            <constructor-arg ref="sessionFactory"/>
        </bean>
    </beans>

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Web Services] Configuration avec Spring xml de services REST
    Par hadydelabe dans le forum Spring
    Réponses: 0
    Dernier message: 09/06/2015, 20h16
  2. [Framework] Configuration de spring pour faire des tests avec maven
    Par wsp_ape dans le forum Spring
    Réponses: 7
    Dernier message: 17/06/2011, 17h12
  3. Réponses: 1
    Dernier message: 18/11/2010, 12h58
  4. [EhCache] Configuration avec Hibernate & Spring
    Par kilicool dans le forum Hibernate
    Réponses: 9
    Dernier message: 23/09/2009, 13h44
  5. [Data] configuration applicationContext spring avec hibernate
    Par riderfun dans le forum Spring
    Réponses: 4
    Dernier message: 25/05/2009, 15h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo