In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the use of ManyToMany one-way and two-way @ JoinTable". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of ManyToMany one-way and two-way @ JoinTable"?
ManyToMany one-way, two-way: @ JoinTable uses one, manytomany one-way
One-way refers to the class level, in the following example, the teacher class can know which students to teach, and the students do not know which teachers to teach.
* * need to use the connection table * * @ JoinTable (name= "tweets", joinColumns= {@ JoinColumn (name= "teacher_id")}, inverseJoinColumns= {@ JoinColumn (name= "student_id")})
You don't have to join the table, but if you don't have to join the table, the table name and column name rules:
If no foreign key is written in the student table, the table name _ primary key name of the other party will be used.
If there is a foreign key in the teacher table, it will s_id (also the primary key name of student)
The table name is student_teacher
Student class
@ Entitypublic 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 class
@ Entity public class teacher {private int id; private String name; private Set s=new HashSet (); @ Id / / must be added to getId @ GeneratedValue public int getId () {return id;} public void setId (int id) {this.id = id } @ ManyToMany @ JoinTable (name= "tacks", joinColumns= {@ JoinColumn (name= "teacher_id")}, inverseJoinColumns= {@ JoinColumn (name= "student_id")}) / / the above two fields will define the name of the intermediate table for the federated primary key / / the column name: joinColumns,inverseJoinColumns is the time to prevent the combination of primary keys. Public Set getS () {return s;} public void setS (Set s) {this.s = s;}} two-way, manytomany
You can also modify the connection table field name with @ JoinTable
@ Entitypublic class student {private int id; private String name; private Set ts=new HashSet (); @ 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= "tweets", joinColumns= {@ JoinColumn (name= "teacher_id")}, inverseJoinColumns= {@ JoinColumn (name= "student_id")} public Set getTs () {return ts;} public void setTs (Set ts) {this.ts = ts;}} @ Entity public class teacher {private int id; private String name; private Set s=new HashSet () @ Id / / must be added to 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") / / defines the name of the intermediate table, column name: joinColumns,inverseJoinColumns is when preventing the combination of primary keys. Summary of public Set getS () {return s;} public void setS (Set s) {this.s = s;} @ ManyToMany (many-to-many relationship)
DeviceGroup class
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;// automatically generates the group_has_ element table in the database, which includes two fields, group_id and element_id, and is mainly used to store the correspondence between the device_ group table and the ne_ element table. The association of @ ManyToMany (cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) / / table to generate an group_has_element intermediate table. @ JoinTable (name = "group_has_element", joinColumns = {@ JoinColumn (name = "group_id", referencedColumnName = "groupId")}, inverseJoinColumns = {@ JoinColumn (name = "element_id", referencedColumnName = "neNeid")} private List elementList; public List getElementList () {return elementList;} public void setElementList (List 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 class
Package com.sunwave.grouping.domain;import javax.persistence.*;import java.io.Serializable;import java.util.Date;import java.util.List;/** * @ author lfw * @ date August 3, 2020 * @ 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; / / device unique ID private String coonReqUrl; / / device connection URL private String serialNumber; / / device serial number private String deviceIp; / / device IP private Long modelId;//modelName corresponding id private String description;// description private String manufacturer;// manufacturer private String softwareVersion / / Software version private Date creationTime;// creation time private Date lastBootstrapTime; private Date lastConnTime;// Last connection time private Date updateTime;// Update time private String oui;//oui private String product; private Boolean authRequirement;// need to authenticate private String dialectIP; private String updateUser;// update user private String macAddress / / mac address private Integer onlineStatus;// online status private Integer sessionStatus;// session status @ ManyToMany (mappedBy = "elementList") private List 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;}}
The above is the use of many-to-many relationship, you can refer to, I encountered the following error message in the process of use:
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 (NativeMethod) ~ [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
There is nothing wrong with checking the entity class and the database. It is because the groupId in my entity class uses the / * @ Column (name= "group_id") annotation, which can be commented out or deleted as follows.
/ * @ Column (name= "group_id") * / private long groupId; Thank you for your reading. The above is the content of "what is the use of ManyToMany one-way and two-way @ JoinTable". After the study of this article, I believe you have a deeper understanding of the use of ManyToMany one-way and two-way @ JoinTable. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.