Answer the question
In order to leave comments, you need to log in
I'm trying to link two tables via @OneToMany, what's wrong?
Phones.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping">
<class name="model.Phones" table="phone">
<id name="IdPhones" type="int" column="id">
<generator class="identity"/>
</id>
<property name="PhoneType" column="phonetype"/>
<property name="PhoneNumber" column="phonenumber"/>
<property name="member" column="person_id"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping">
<class name="model.Members" table="person">
<id name="Specialist" column="id">
<generator class="identity"/>
</id>
<property name="Name" column="name"/>
<property name="Surname" column="surname"/>
<property name="SecondName" column="secondname"/>
<property name="Address" column="address"/>
<property name="Birthdate" column="birthdate"/>
</class>
</hibernate-mapping>
package model;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "phone")
public class Phones {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int IdPhones;
@Column(name = "phonetype")
private String PhoneType;
@Column(name = "phonenumber")
private int PhoneNumber;
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,optional=true)
@JoinColumn(name = "person_id", nullable = false)
private Members member;
public Members getMember() {
return member;
}
public void setMember(Members member) {
this.member = member;
}
public int getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
PhoneNumber = phoneNumber;
}
public String getPhoneType() {
return PhoneType;
}
public void setPhoneType(String phoneType) {
PhoneType = phoneType;
}
public int getIdPhones() {
return IdPhones;
}
public void setIdPhones(int idPhones) {
IdPhones = idPhones;
}
@Override
public String toString() {
return "Phones" +"\n"
+"phoneType = " + PhoneType + "\n"
+"phoneNumber = " + PhoneNumber + "\n"
;
}
}
package model;
import lombok.*;
import javax.persistence.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "person")
public class Members {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int Specialist;
@Column(name = "name")
private String Name;
@Column(name = "surname")
private String Surname;
@Column(name = "secondname")
private String SecondName;
@Column(name = "address")
private String Address;
@Column(name = "birthday")
private String Birthdate;
@OneToMany(targetEntity = Phones.class, mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<Phones> phones;
public Set<Phones> getPhones() {
return phones;
}
public void setPhones(Set<Phones> phones) {
this.phones = phones;
}
public void addPhone(Phones phone){
phones.add(phone);
phone.setMember(this);
}
public void removePhone(Phones phone) {
phones.remove( phone );
phone.setMember( null );
}
public String getBirthdate() {
return Birthdate;
}
public void setBirthdate(String birthdate) {
Birthdate = birthdate;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getSecondName() {
return SecondName;
}
public void setSecondName(String secondName) {
SecondName = secondName;
}
public String getSurname() {
return Surname;
}
public void setSurname(String surname) {
Surname = surname;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getSpecialist() {
return Specialist;
}
public void setSpecialist(int specialist) {
Specialist = specialist;
}
@Override
public String toString() {
return "Members" +"\n"
+"Name = " + Name + "\n"
+"Surname = " + Surname + "\n"
+"SecondName = " + SecondName + "\n"
+"Birthdate = " + Birthdate + "\n"
+"Address = " + Address;
}
}
package dao;
import com.sun.istack.NotNull;
import model.Members;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class MembersDAO implements DAO<Members, Integer> {
/**
* Connection factory to database.
*/
private final SessionFactory factory;
public MembersDAO(@NotNull final SessionFactory factory) {
this.factory = factory;
}
/**
* Create new model in models table.
*
* @param members for add.
*/
@Override
public void create(@NotNull final Members members) {
try (final Session session = factory.openSession()) {
session.beginTransaction();
session.save(members);
session.getTransaction().commit();
}
}
/**
* Get model by car.
*
* @param Specialist for select.
* @return Specialist with param model.
*/
@Override
public Members read(@NotNull final Integer Specialist) {
try (final Session session = factory.openSession()) {
final Members result = session.get(Members.class, Specialist);
return result != null ? result : new Members();
}
}
/**
* Update model state.
*
* @param members new state.
*/
@Override
public void update(@NotNull final Members members) {
try (Session session = factory.openSession()) {
session.beginTransaction();
session.update(members);
session.getTransaction().commit();
}
}
/**
* Delete model.
*
* @param members for delete.
*/
@Override
public void delete(@NotNull final Members members) {
try (Session session = factory.openSession()) {
session.beginTransaction();
session.delete(members);
session.getTransaction().commit();
}
}
}
public class ApplicationMembersDemo {
public static void main(String[] args) {
SessionFactory factory = null;
try {
factory = new Configuration().configure().buildSessionFactory();
DAO<Members, Integer> dao = new MembersDAO(factory);
//create(dao);
//read(dao);
//update(dao);
//delete(dao);
//create2(dao);
create3(dao);
} finally {
if (factory != null) {
factory.close();
}
}
}
private static void create3(DAO<Members, Integer> membersDAO) {
Members members = new Members();
members.setName("test-mark");
members.setSurname("test-mark");
members.setSecondName("test-mark");
members.setAddress("test-mark");
members.setBirthdate("test-mark");
Phones phone1 = new Phones();
phone1.setPhoneType("Home");
phone1.setPhoneNumber(8999566);
phone1.setMember(members);
members.getPhones().add(phone1);
Phones phone2 = new Phones();
phone2.setPhoneType("Work");
phone2.setPhoneNumber(8941417);
phone2.setMember(members);
members.addPhone(phone2);
membersDAO.create(members);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question