ManyToMany單向、雙向:@JoinTable的使用

ManyToMany單向、雙向:@JoinTable使用

一、manytomany單向

單向是指類層面,在下面例子中老師類可以知道要教哪些學生,學生不知被哪些老師教

**需要用到連接表**@JoinTable(name="t_s",
joinColumns={@JoinColumn(name="teacher_id")},inverseJoinColumns={@JoinColumn(name="student_id")}
)

也可以不用連接表,但是若不用連接表,表名、列名規則:

  • 在student表中沒有寫外鍵則會用對方的表名_主鍵名
  • 在teacher表中有外鍵,則會s_id(也是student的主鍵名)
  • 表名是student_teacher

student類

@Entity
public class student {
    private int id;
    private String name;
    @Id
    @GeneratedValue
    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;
    }
}

Teacher類

@Entity 
public class teacher {
    private int id;
    private String name;
    private Set<student> s=new HashSet<student>();
    @Id //必須加在getId上面
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @ManyToMany
    @JoinTable(name="t_s",
    joinColumns={@JoinColumn(name="teacher_id")},inverseJoinColumns={@JoinColumn(name="student_id")}
    )//將會以上兩個字段為聯合主鍵
    //定義中間表的名稱,列名:joinColumns,inverseJoinColumns是預防組合主鍵的時候。
    public Set<student> getS() {
        return s;
    }
    public void setS(Set<student> s) {
        this.s = s;
    }   
}

二、manytomany雙向

也可以用@JoinTable對連接表字段名進行修改

@Entity
public class student {
    private int id;
    private String name;
    private Set<teacher> ts=new HashSet<teacher>();
    @Id
    @GeneratedValue
    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;
    }
    @ManyToMany
    @JoinTable(name="t_s",
    joinColumns={@JoinColumn(name="teacher_id")},inverseJoinColumns={@JoinColumn(name="student_id")}
    )
    public Set<teacher> getTs() {
        return ts;
    }
    public void setTs(Set<teacher> ts) {
        this.ts = ts;
    }
}
@Entity 
public class teacher {
    private int id;
    private String name;
    private Set<student> s=new HashSet<student>();
    @Id //必須加在getId上面
    @GeneratedValue
    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;
    }
    @ManyToMany(mappedBy="ts")
    //定義中間表的名稱,列名:joinColumns,inverseJoinColumns是預防組合主鍵的時候。
    public Set<student> getS() {
        return s;
    }
    public void setS(Set<student> s) {
        this.s = s;
    }
}

@ManyToMany(多對多關系)使用小結

DeviceGroup類

package com.sunwave.grouping.domain;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Entity
@Table(name = "device_group")
public class DeviceGroup implements Serializable{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
   /* @Column(name="group_id")*/
    private long groupId;
    @Column(name="group_name")
    private String groupName;
    @Column(name="description")
    private String description;//自動在數據庫裡生成瞭group_has_element表,該表包括group_id和element_id兩個字段,該表主要是用於存儲device_group表和ne_element表的對應關系。
    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)//表的關聯,生成一個group_has_element中間表。
    @JoinTable(name = "group_has_element",joinColumns = {@JoinColumn(name = "group_id",referencedColumnName = "groupId")}
    ,inverseJoinColumns = {@JoinColumn(name = "element_id",referencedColumnName = "neNeid")})
    private List<NeElement> elementList;
    
    public List<NeElement> getElementList() {
		return elementList;
	}
	public void setElementList(List<NeElement> elementList) {
		this.elementList = elementList;
	}
	public long getGroupId() {
		return groupId;
	}
	public void setGroupId(long groupId) {
		this.groupId = groupId;
	}
	public String getGroupName() {
		return groupName;
	}
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	@Override
	public String toString() {
		return "DeviceGroup [groupId=" + groupId + ", groupName=" + groupName + ", description=" + description
				+ ", elementList=" + elementList + "]";
	}
	
    
    
}

NeElement 類

package com.sunwave.grouping.domain;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
 * @author lfw
 * @date   2020年8月3日
 * @time   下午6:44:54
 * 
 */
@Entity
@Table(name = "ne_element")
public class NeElement implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long neNeid; //設備唯一標識
	private String coonReqUrl; //設備連接URL
	private String serialNumber; //設備序列號
	private String deviceIp;  //設備IP
	private Long modelId;//modelName對應的id
	private String description;//描述
	private String manufacturer;//制造商
	private String softwareVersion;//軟件版本
	private Date creationTime;//創建時間
	private Date lastBootstrapTime;
	private Date lastConnTime;//最後連接時間
	private Date updateTime;//更新時間
	private String oui;//oui
	private String product;
	private Boolean authRequirement;//是否需要認證
	private String dialectIP;
	private String updateUser;//更新用戶
	private String macAddress;//mac地址
	private Integer onlineStatus;//在線狀態
	private Integer sessionStatus;//會話狀態
   @ManyToMany(mappedBy = "elementList")
	private List<DeviceGroup> deviceGroupList;
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getManufacturer() {
		return manufacturer;
	}
	public void setManufacturer(String manufacturer) {
		this.manufacturer = manufacturer;
	}
	public String getSoftwareVersion() {
		return softwareVersion;
	}
	public void setSoftwareVersion(String softwareVersion) {
		this.softwareVersion = softwareVersion;
	}
	public Date getLastConnTime() {
		return lastConnTime;
	}
	public void setLastConnTime(Date lastConnTime) {
		this.lastConnTime = lastConnTime;
	}
	public Long getModelId() {
		return modelId;
	}
	public void setModelId(Long modelId) {
		this.modelId = modelId;
	}
	public Long getNeNeid() {
		return neNeid;
	}
	public void setNeNeid(Long neNeid) {
		this.neNeid = neNeid;
	}
	public String getCoonReqUrl() {
		return coonReqUrl;
	}
	public void setCoonReqUrl(String coonReqUrl) {
		this.coonReqUrl = coonReqUrl;
	}
	public String getDeviceIp() {
		return deviceIp;
	}
	public void setDeviceIp(String deviceIp) {
		this.deviceIp = deviceIp;
	}
	public String getSerialNumber() {
		return serialNumber;
	}
	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}
	public Date getCreationTime() {
		return creationTime;
	}
	public void setCreationTime(Date creationTime) {
		this.creationTime = creationTime;
	}
	public Date getLastBootstrapTime() {
		return lastBootstrapTime;
	}
	public void setLastBootstrapTime(Date lastBootstrapTime) {
		this.lastBootstrapTime = lastBootstrapTime;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	public String getOui() {
		return oui;
	}
	public void setOui(String oui) {
		this.oui = oui;
	}
	public String getProduct() {
		return product;
	}
	public void setProduct(String product) {
		this.product = product;
	}
	public Boolean getAuthRequirement() {
		return authRequirement;
	}
	public void setAuthRequirement(Boolean authRequirement) {
		this.authRequirement = authRequirement;
	}
	public String getDialectIP() {
		return dialectIP;
	}
	public void setDialectIP(String dialectIP) {
		this.dialectIP = dialectIP;
	}
	public String getUpdateUser() {
		return updateUser;
	}
	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}
	public String getMacAddress() {
		return macAddress;
	}
	public void setMacAddress(String macAddress) {
		this.macAddress = macAddress;
	}
	public Integer getOnlineStatus() {
		return onlineStatus;
	}
	public void setOnlineStatus(Integer onlineStatus) {
		this.onlineStatus = onlineStatus;
	}
	public Integer getSessionStatus() {
		return sessionStatus;
	}
	public void setSessionStatus(Integer sessionStatus) {
		this.sessionStatus = sessionStatus;
	}
}

以上為多對多關系的使用,大傢可以參考,本人在使用過程中遇到以下報錯信息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to map collection com.sunwave.grouping.domain.DeviceGroup.elementList
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087) ~[spring-context-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) ~[spring-context-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE]
 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE]
 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.8.RELEASE.jar:2.0.8.RELEASE]
 at com.sunwave.Application.main(Application.java:26) [classes/:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
 at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
 at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.8.RELEASE.jar:2.0.8.RELEASE]
Caused by: org.hibernate.AnnotationException: Unable to map collection com.sunwave.grouping.domain.DeviceGroup.elementList
 at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1621) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1352) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:810) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:735) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1640) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1608) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690) ~[spring-beans-5.0.12.RELEASE.jar:5.0.12.RELEASE]
 … 21 common frames omitted
Caused by: org.hibernate.cfg.RecoverableException: Unable to find column with logical name: groupId in org.hibernate.mapping.Table(device_group) and its related supertables and secondary tables
 at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:837) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:244) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1611) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 … 37 common frames omitted
Caused by: org.hibernate.MappingException: Unable to find column with logical name: groupId in org.hibernate.mapping.Table(device_group) and its related supertables and secondary tables
 at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:832) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
 … 39 common frames omitted

檢查瞭實體類和數據庫發現沒有什麼錯,原來是因為我的實體類裡的groupId上用瞭/*@Column(name=“group_id”)註解,如下把該註解註釋掉或者刪除掉就可以瞭。

/* @Column(name="group_id")*/ private long groupId;

其他關於@ManyToMany(多對多關系)的使用可以去百度再看看,本人就不多說瞭。

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: