私は休止状態の初心者です。いくつかのチュートリアルを見てきましたが、この問題が見つかりません:
- 既に存在する別のテーブルとの依存関係があるテーブルに新しい行を追加するにはどうすればよいですか?
hibernate.cfg.xml
> <?xml version='1.0' encoding='utf-8'?>
> <!DOCTYPE hibernate-configuration PUBLIC
> "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
> "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
>
> <hibernate-configuration>
> <session-factory>
> <!-- Paramètres de connexion à la base de données -->
> <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
> <!-- <property name="connection.url">jdbc:mysql://localhost:3306/bh</property> -->
> <!-- <property name="connection.username">root</property> -->
> <!-- <property name="connection.password"></property> -->
> <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
>
> <property name="connection.driver_class">org.postgresql.Driver</property>
> <property name="connection.url">jdbc:postgresql://localhost:5432/projetForum</property>
> <property name="connection.username">postgres</property>
> <property name="connection.password">esct</property>
> <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
>
> <!-- Comportement pour la conservation des tables -->
> <property name="hbm2ddl.auto">update</property>
>
> <!-- Activation : affichage en console, commentées et formatées -->
> <property name="show_sql">true</property>
> <property name="hibernate.format_sql">true</property>
> <property name="use_sql_comments">true</property>
>
> <!-- Fichiers à mapper -->
> <mapping class="com.forum.beans.Utilisateur" />
> <mapping class="com.forum.beans.Topic" />
> <mapping class="com.forum.beans.Post" />
>
> </session-factory>
> </hibernate-configuration>
私の豆のクラス
package com.forum.beans; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.xml.bind.annotation.XmlTransient; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @Entity @Table(name = "utilisateur") public class Utilisateur { @Id @Column(name = "utilisateur_email") private String email; @Column(name = "utilisateur_mdp") private String motDePasse; @Column(name = "utilisateur_nom") private String nom; @Column(name = "utilisateur_avatar") private String avatar; @Column(name = "utilisateur_localisation") private String localisation; @Column(name = "utilisateur_siteweb") private String siteweb; @Column(name = "utilisateur_dateInscrit") private String dateInscrit; @Column(name = "utilisateur_dateDernVisite") private String dateDernVisite; @Column(name = "utilisateur_description") private String description; @Column(name = "utilisateur_dateNaiss") private String dateDeNaissance; @OneToMany(mappedBy = "tcreateur") @LazyCollection(LazyCollectionOption.FALSE) private List<Topic> topicList; @OneToMany(mappedBy = "pcreateur") @LazyCollection(LazyCollectionOption.FALSE) private List<Post> postList; public Utilisateur(String email, String motDePasse, String nom, String localisation, String siteweb, String description) { this.email = email; this.motDePasse = motDePasse; this.nom = nom; this.topicList = null; this.postList = null; this.siteweb = siteweb; this.description = description; this.localisation = localisation; SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); Date aujourdhui = new Date(); this.dateDernVisite = formater.format(aujourdhui); } public Utilisateur() { } public String getDateDernVisite() { return dateDernVisite; } public void setDateDernVisite(String dateDernVisite) { this.dateDernVisite = dateDernVisite; } public String getDateDeNaissance() { return dateDeNaissance; } public void setDateDeNaissance(String dateDeNaissance) { this.dateDeNaissance = dateDeNaissance; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } public String getLocalisation() { return localisation; } public void setLocalisation(String localisation) { this.localisation = localisation; } public String getSiteweb() { return siteweb; } public void setSiteweb(String siteweb) { this.siteweb = siteweb; } public String getDateInscrit() { return dateInscrit; } public void setDateInscrit(String dateInscrit) { this.dateInscrit = dateInscrit; } public String getdateDernVisite() { return dateDernVisite; } public void setdateDernVisite(String dateDernVisite) { this.dateDernVisite = dateDernVisite; } @XmlTransient public List<Post> getPostList() { return postList; } public void setPostList(List<Post> postList) { this.postList = postList; } @XmlTransient public List<Topic> getTopicList() { return topicList; } public void setTopicList(List<Topic> topicList) { this.topicList = topicList; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } public void setMotDePasse(String motDePasse) { this.motDePasse = motDePasse; } public String getMotDePasse() { return motDePasse; } public void setNom(String nom) { this.nom = nom; } public String getNom() { return nom; } }
そして、私が追加しようとしているクラス:
> package com.forum.beans;
>
> import java.text.SimpleDateFormat;
> import java.util.Date;
> import java.util.List;
>
> import javax.persistence.CascadeType;
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.JoinColumn;
> import javax.persistence.ManyToOne;
> import javax.persistence.OneToMany;
> import javax.persistence.Table;
> import javax.xml.bind.annotation.XmlTransient;
>
> import org.hibernate.annotations.LazyCollection;
> import org.hibernate.annotations.LazyCollectionOption;
>
> @Entity
> @Table(name = "topic")
> public class Topic {
>
> @Id
> @Column(name = "topic_id")
> @GeneratedValue(strategy = GenerationType.AUTO)
> private Integer id;
> @Column(name = "topic_titre")
> private String titre;
> @Column(name = "topic_corps")
> private String corps;
> @Column(name = "topic_date")
> private String date;
>
> @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
> @JoinColumn(name = "topic_createur")
> private Utilisateur tcreateur;
>
> @OneToMany(mappedBy = "ptopic")
> @LazyCollection(LazyCollectionOption.FALSE)
> private List<Post> tpostList;
>
> public Topic(String titre, String corps) {
> this.titre = titre;
> this.corps = corps;
>
> SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
> Date aujourdhui = new Date();
> this.date = formater.format(aujourdhui);
>
> }
>
> public Topic() {
> }
>
> @XmlTransient
> public List<Post> getTpostList() {
> return tpostList;
> }
>
> public void setTpostList(List<Post> tpostList) {
> this.tpostList = tpostList;
> }
>
> @XmlTransient
> public List<Post> getPostList_topic() {
> return tpostList;
> }
>
> public void setPostList_topic(List<Post> tpostList) {
> this.tpostList = tpostList;
> }
>
> public void setDate(String date) {
> this.date = date;
> }
>
> public void setTcreateur(Utilisateur tcreateur) {
> this.tcreateur = tcreateur;
> }
>
> public String getDate() {
> return date;
> }
>
> public void setDateDernModif(String date) {
> this.date = date;
> }
>
> public Utilisateur getTcreateur() {
> return tcreateur;
> }
>
> public void setEditeur(Utilisateur tcreateur) {
> this.tcreateur = tcreateur;
> }
>
> public Integer getId() {
> return id;
> }
>
> public void setId(Integer id) {
> this.id = id;
> }
>
> public String getTitre() {
> return titre;
> }
>
> public void setTitre(String titre) {
> this.titre = titre;
> }
>
> public String getCorps() {
> return corps;
> }
>
> public void setCorps(String corps) {
> this.corps = corps;
> }
> }
私のメインクラスの一部:
> Utilisateur utilisateur = (Utilisateur) session
> .getAttribute("utilisateur");
>
> TopicDAO<com.forum.beans.Topic, Integer> ts = new TopicDAO<com.forum.beans.Topic, Integer>();
> TopicForm form = new TopicForm();
> com.forum.beans.Topic t = form.creerSujet(request);
> List<com.forum.beans.Topic> tl =utilisateur.getTopicList();
>
> try {
> if (utilisateur.getTopicList() == null)
> tl = (List<com.forum.beans.Topic>) new ArrayList<com.forum.beans.Topic>();
> else
> tl = utilisateur.getTopicList();
> } catch (Exception e) {
> System.out.println(e);
> }
>
> tl.add(t);
> t.setTcreateur(utilisateur);
> utilisateur.setTopicList(tl);
> ts.create(t);
したがって、私の問題は、Topic クラスを永続化することですが、 Utilisateur クラスが DB に既に存在するという JDBC 例外が発生することです。
完全なスタックトレース
22:44:48,476 WARN JDBCExceptionReporter:100 - SQL エラー: 0、SQLState: 23505 22:44:48,477 ERROR JDBCExceptionReporter:101 - L'élément du batch 0 /* com.forum.beans.Utilisateur を挿入 */ utilisateur に挿入 ( utilisateur_avatar, utilisateur_dateNaiss, utilisateur_dateDernVisite, utilisateur_dateInscrit, utilisateur_description, utilisateur_localisation, utilisateur_mdp, utilisateur_nom, utilisateur_siteweb, utilisateur_email) values (NULL, NULL, '16-08-2013 10:44:42', '16-08-2013 10:04:04 '、NULL、NULL、'mee'、'testerMan'、NULL、'me@mail.com') は廃止されました。Appeler getNextException は原因を突き止めます。22:44:48,477 WARN JDBCExceptionReporter:100 - SQL エラー: 0、SQLState: 23505 22:44:48,478 エラー JDBCExceptionReporter:101 - エラー: la valeur d' une clé dupliquée rompt la contrainte unique « utilisateur_pkey » 詳細 : La clé « (utilisateur_email)=(me@mail.com) » 存在します。22:44:48,482 エラー AbstractFlushingEventListener:324 - データベースの状態をセッション org.hibernate.exception.ConstraintViolationException と同期できませんでした: 組織で org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) で JDBC バッチ更新を実行できませんでした.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114) org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109) を org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher. run(Unknown Source) at java.lang.Thread.run(Unknown Source) 原因: java.sql.BatchUpdateException: L'élément du batch 0 /* insert com.forum.beans.Utilisateur */ insert into utilisateur (utilisateur_avatar, utilisateur_dateNaiss、utilisateur_dateDernVisite、utilisateur_dateInscrit、utilisateur_description、utilisateur_localisation、utilisateur_mdp、utilisateur_nom、utilisateur_siteweb、utilisateur_email) 値 (NULL、NULL、'16-08-2013 10:40:42'、'-2013:4:4:18' NULL, NULL, 'mee', 'testerMan', NULL, 'me@mail.com') は廃止されました。Appeler getNextException は原因を突き止めます。org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2746) で org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.