Hibernate不会在数据库中生成外键

时间:2018-08-20 13:26:14

标签: java hibernate jdbc

我在表“人”和地址之间创建了多对一关系,给定了我的ER图,它似乎已创建。但这并没有使我的数据源发生任何事情,没有连接就没有

是否有办法做到这一点?或者应该像Intellij那样?使用Eclipse似乎很容易。

这是我对实体的代码,也是我认为相关的任何东西,请随时询问是否需要任何东西。

@Entity
@Table(name = "personne", schema = "bddpersistence")
@IdClass(PersonneEntityPK.class)
public class PersonneEntity {
    private int idPersonne;
    private String nom;
    private String prenom;
    private String email;
    private String tel;
    private Integer age;
    private Integer idAdress;

    @OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,orphanRemoval = true)
    private List<AdressEntity> adress = new ArrayList<AdressEntity>();

@Entity
@Table(name = "adress", schema = "bddpersistence")
public class AdressEntity {
    private String rue;
    private Integer codePostal;
    private String ville;
    private int idAdress;

    @ManyToOne
    PersonneEntity personne;

package com.example;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.persistence.*;
import java.sql.Timestamp;


public class TestHibernate {

    static EntityManagerFactory emf;
    static EntityManager em;
    static EntityTransaction transaction;

    public static void beginConnection(){

        emf = Persistence.createEntityManagerFactory("NewPersistenceUnit");
        em = emf.createEntityManager();
        transaction = em.getTransaction();
        transaction.begin();

    }

    public static void endConnection(){

        transaction.commit();
        em.close();
        emf.close();

    }

    public static void insertAccount(String email, String name){
        ComptesEntity compte = new ComptesEntity();
        compte.setEmail(email);
        compte.setNom(name);
        em.persist(compte);
    }

    public static void createPerson (int id_personne,String nom,String prenom,int age,String email,String tel,String rue,String cp,String ville){

    }

    public static void insertPerson(String lastName, String firstName , String email, String tel , int age){
        PersonneEntity personne = new PersonneEntity();
        personne.setNom(lastName);
        personne.setPrenom(firstName);
        personne.setTel(tel);
        personne.setEmail(email);
        personne.setAge(age);
        em.persist(personne);
    }

    public static void main(String[] args) {
        beginConnection();
        endConnection();
    }
}

这是persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">

    <persistence-unit name="NewPersistenceUnit">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/bddpersistence"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="kamoulox369"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="create-drop"/>
        </properties>
    </persistence-unit>
</persistence>

这是休眠配置:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:mysql://localhost:3306/bddpersistence</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="show_sql">true</property>
    <mapping class="com.example.AdressEntity"/>
    <mapping resource="com/example/AdressEntity.hbm.xml"/>
    <mapping class="com.example.ClubEntity"/>
    <mapping resource="com/example/ClubEntity.hbm.xml"/>
    <mapping class="com.example.ComptesEntity"/>
    <mapping resource="com/example/ComptesEntity.hbm.xml"/>
    <mapping class="com.example.ParticipationEntity"/>
    <mapping resource="com/example/ParticipationEntity.hbm.xml"/>
    <mapping class="com.example.PersonneEntity"/>
    <mapping resource="com/example/PersonneEntity.hbm.xml"/>

    <!-- <property name="connection.username"/> -->
    <!-- <property name="connection.password"/> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

最后是PersonEntity的复合PK:

package com.example;

import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Objects;

public class PersonneEntityPK implements Serializable {
    private int idPersonne;
    private String email;

    @Column(name = "id_personne")
    @Id
    public int getIdPersonne() {
        return idPersonne;
    }

    public void setIdPersonne(int idPersonne) {
        this.idPersonne = idPersonne;
    }

    @Column(name = "email")
    @Id
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PersonneEntityPK that = (PersonneEntityPK) o;
        return idPersonne == that.idPersonne &&
                Objects.equals(email, that.email);
    }

    @Override
    public int hashCode() {

        return Objects.hash(idPersonne, email);
    }
}

我为刚生成的实体(如getter和setter)遗留了部分代码。

0 个答案:

没有答案