2016-09-18 1 views
0

Hallo Alle erstellen eine zu viele realtionship. Ich habe 2 Tabelle eins ist Emp und andere ist Project. Ein Emp kann viele Projekt haben. Das sind meine Bohnenklasse.überwintern eins zu viele Beziehung, Kind Tabelle Foriegn Schlüssel Tabelle Einstellung 0 Wert

public class Emp { 

    public List<Project> getProjectList() { 
     return projectList; 
    } 

    public void setProjectList(List<Project> projectList) { 
     this.projectList = projectList; 
    } 

    @Generated(value = { "id" }) 
    @Id 
    private int id; 

    @Column(name="name") 
    private String name; 

    @Column(name="city") 
    private String city; 

    @OneToMany(cascade=CascadeType.ALL , fetch=FetchType.LAZY, mappedBy="emp",orphanRemoval=true) 
    private List<Project> projectList=new ArrayList<Project>(); 


    public void addProject(Project project) { 
     this.projectList.add(project); 
     if (project.getEmp() != this) { 
      project.setEmployer(this); 
     } 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

} 

Dies ist die Projektklasse

@Entity 
     public class Project { 




     @Generated(value = { "id" }) 
     @Id 
     private int id; 

     @Column(name="name") 
     private String name; 

     @ManyToOne(cascade=CascadeType.ALL) 
     @JoinColumn(name="empid", nullable=false) 
     private Emp emp; 


     public int getId() { 
      return id; 
     } 


     public void setId(int id) { 
      this.id = id; 
     } 


     public String getName() { 
      return name; 
     } 


     public void setName(String name) { 
      this.name = name; 
     } 


     public Emp getEmp() { 
      return emp; 
     } 


     public void setEmp(Emp emp) { 
      this.emp = emp; 
     } 


     public void setEmployer(Emp emp2) { 
      this.emp = emp2; 
       if (!emp2.getProjectList().contains(this)) { // warning this may cause performance issues if you have a large data set since this operation is O(n) 
        emp.getProjectList().add(this); 
       } 


     } 

    } 

Das ist mein Code ist es, die Eltern und Kind

public class MainTest { 

public static void main(String[] args) { 
    Configuration configuration = new Configuration(); 
    configuration.addAnnotatedClass(org.hibernate.model.Emp.class); 
    configuration.addAnnotatedClass(org.hibernate.model.Project.class); 
    configuration.addAnnotatedClass(org.hibernate.model.Stu.class); 
    configuration.addAnnotatedClass(org.hibernate.model.Address.class); 



    configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); 
    configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/test"); 
    configuration.setProperty("hibernate.connection.password", "123456789"); 
    configuration.setProperty("hibernate.connection.username", "root"); 
    configuration.setProperty("hibernate.show_sql", "true"); 
    configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 
    SessionFactory sessionFactory = configuration.buildSessionFactory(); 


Session session = sessionFactory.openSession(); 
org.hibernate.Transaction transaction =session.beginTransaction(); 

Emp emp = new Emp(); 
emp.setCity("nagaland"); 
emp.setName("divyffa"); 

Project project1=new Project(); 
project1.setName("dotedxvc"); 
emp.addProject(project1); 
session.save(emp); 
transaction.commit(); 
session.close(); 



} 


} 

Dies ist die Struktur meiner Tabelle

 CREATE TABLE `emp` (

     `id` int(11) NOT NULL AUTO_INCREMENT, 
     `name` varchar(45) DEFAULT NULL, 
     `city` varchar(45) DEFAULT NULL, 
     PRIMARY KEY (`id`) 
    ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8; 


    CREATE TABLE `project` (
     `id` int(11) NOT NULL AUTO_INCREMENT, 
     `name` varchar(45) DEFAULT NULL, 
     `empid` int(11) NOT NULL, 
     PRIMARY KEY (`id`), 

    KEY `forgn-project_idx` (`empid`) 
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8; 

zu sparen Immer wenn ich meine Elternklasse rette, meine Kind-Klasse wird auch gespeichert, aber die Empid Spalte Wert wird als 0 gespeichert. Jedes Mal, wenn es als 0 gespeichert wird.

Bitte ich weiß nicht, was das Problem ist und danke für die Hilfe. Ich habe nach diesem Problem gesucht, aber kein Ergebnis gefunden.

Antwort

1

Es scheint, dass Sie nur eine Seite der Beziehung festgelegt haben. Ich denke, etwas wie unten brauchen hier

project1.setEmp (emp);

+0

Haben Sie gesehen, meine pojo.Ich habe Add-Methode, die mein Objekt auf der Seite hinzugefügt.Es synchronisiert die beiden Objekte –

+0

Entschuldigung mein bad..ein anderer Punkt zu konzentrieren ist mit einem Abruf-Typ @ManyToOne (fetch = FetchType.LAZY) – user3014595

0

Ich denke Ihre FOREIGN KEY-Spalte sollte beziehen sich auf die übergeordnete Tabelle bedeutet "emp" -Tabelle PRIMARY KEY-Spalte. Ich habe Kind modifizierte Tabelle bedeutet "Projekt" Struktur wie-

CREATE TABLE `project` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`name` varchar(45) DEFAULT NULL, 
`empid` int(11) NOT NULL, 
PRIMARY KEY (`id`), 
FOREIGN KEY (empid) REFERENCES emp (id) 
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

In Emp Entitätsklasse @OneToMany Mapping as-

@OneToMany (fetch = FetchType.EAGER, kaskaden = CascadeType.ALL, mappedBy add = "emp") private Liste projectList;

und auch in der Klasse Projekt Einheit @ManyToOne Mapping ändern as-

@ManyToOne(optional = false) 
@JoinColumn(name = "empid", insertable = true) 
private Emp emp; 

Ich denke, das wird für Sie arbeiten.

Verwandte Themen