JPA:Repeated column in mapping for entity

Pit: from this new entity class (New - .. JPA entities from tables)

Probably cause three errors: one is the title of this ① A ② the effect that there must be at least a non-read-only column (effect Keywords non-read only, There should be one non-read-only mapping defined for the primary key field), to the effect that there is a wrong ③update like.

All because of this error.

When you generate such entity, need to pay attention to the following issues:

Relationship when there are many associations that must be saved with a corresponding table (there are more than one student teacher, students have more teachers), at this time, so be sure the table name: ref_teacher_student, so this table also selected does not generate a real entity, but to build relationships such join_table.

Meanwhile title listed above to errors because of the following reasons:

As: elective table inside the primary key is the student ID + course ID, time, JPA automatically you create StudentClassPK primary key class, inside sid and cid containing this one (insertable = false, updatable = false), and in the course selection table corresponding to entity, in addition to the primary key and associated attributes, as well as Student Class object and, when there is no JoinColumn (insertable = false, updatable = false) of.

Such direct operation will cause an error in the title;

If JoinColumn inside with insertable = false, updatable = false, it will produce the second error

The method of solving the above problems is the PK of the insertable = false, updatable = false change table corresponding to the enrollment entity, and later Student Class object JoinColumn inside.

After resolving the following code (here code User, Test Examples of the) ADMIN no relationship table and other tables, it did not participate in the above described

Automatically generated after the contact:

After automatically generating entity classes:

以UserTest为例,说明关于insertable=false的修改

package entity;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the user_test database table.
 * 
 */
@Entity
@Table(name="user_test")
@NamedQuery(name="UserTest.findAll", query="SELECT u FROM UserTest u")
public class UserTest implements Serializable {
	private static final long serialVersionUID = 1L;

	@EmbeddedId
	private UserTestPK id;

	private int issubmit;

	private int score;

	//bi-directional many-to-one association to Test
	@ManyToOne
	@JoinColumn(name="tid",insertable=false, updatable=false)
	private Test test;

	//bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name="uid",insertable=false, updatable=false)
//这里的insertable=false等原来在PK类里面的uid和tid里面
	private User user;

	public UserTest() {
	}

	public UserTestPK getId() {
		return this.id;
	}

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

	public int getIssubmit() {
		return this.issubmit;
	}

	public void setIssubmit(int issubmit) {
		this.issubmit = issubmit;
	}

	public int getScore() {
		return this.score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public Test getTest() {
		return this.test;
	}

	public void setTest(Test test) {
		this.test = test;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
package entity;

import java.io.Serializable;
import javax.persistence.*;

/**
 * The primary key class for the user_test database table.
 * 
 */
@Embeddable
public class UserTestPK implements Serializable {
	//default serial version id, required for serializable classes.
	private static final long serialVersionUID = 1L;

	@Column()
	private int tid;

	@Column()
//这里自动生成的时候有insertable=false等
	private String uid;

	public UserTestPK() {
	}
	public int getTid() {
		return this.tid;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public String getUid() {
		return this.uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}

	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof UserTestPK)) {
			return false;
		}
		UserTestPK castOther = (UserTestPK)other;
		return 
			(this.tid == castOther.tid)
			&& this.uid.equals(castOther.uid);
	}

	public int hashCode() {
		final int prime = 31;
		int hash = 17;
		hash = hash * prime + this.tid;
		hash = hash * prime + this.uid.hashCode();
		
		return hash;
	}
}

注意:本质上User和Test、User和Submit(对应TEST的SUBMIT)都是多对多关系,为啥拆成两个一对多,在中间加上了UserTest和UserSubmit是因为需要保存一些值,如时间、分数等,Test和Question是多对多,是因为没有保存这些关联时的一些属性,如时间等。

因此,对于数据库表的设计,更多的是看所谓的实际需要,没有绝对的关联关系和表结构设计,只要是合理的,那么就应该给予支持。

发布了32 篇原创文章 · 获赞 5 · 访问量 4655

Guess you like

Origin blog.csdn.net/qq_38941327/article/details/100547207