Hibernate对模型中的类的两个引用

时间:2018-07-24 07:37:03

标签: java hibernate

我在创建一个具有对同一类的两个引用的模型时遇到问题。

模型项:

@Entity
public class Item {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String subjectName;
private String grade;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "student_id", nullable = false)
private User student;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "teacher_id", nullable = false)
private User teacher;
}

模型用户:

public class User{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String surname;
private String username;

@Email
private String email;
private String password;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles",
        joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();

private String street;
private String numberStreet;
private String postalCode;
private String city;

@OneToMany(
        mappedBy = "student",
        cascade = CascadeType.ALL,
        fetch = FetchType.EAGER,
        orphanRemoval = true
)
private Set<Item> items = new HashSet<>();

@OneToMany(mappedBy = "teacher",
        cascade = CascadeType.ALL,
        fetch = FetchType.EAGER,
        orphanRemoval = true
)
private Set<Item> carriedItems = new HashSet<>();

我想这样做,以便该项目引用两个用户(学生,老师),因为该用户具有角色并且不想创建两个模型(学生,老师),目前我有一个问题在休眠模式下搜索用户时,我认为这些注释出了点问题。 我不知道怎么了

每秒发送数百个查询:

2018-07-24 09:21:02.125 `DEBUG 312 --- [nio-5000-exec-1] org.hibernate.SQL                        : select items0_.student_id as student_6_0_0_, items0_.id as id1_0_0_, items0_.id as id1_0_1_, items0_.created_at as created_2_0_1_, items0_.updated_at as updated_3_0_1_, items0_.grade as grade4_0_1_, items0_.student_id as student_6_0_1_, items0_.subject_name as subject_5_0_1_, items0_.teacher_id as teacher_7_0_1_ from items items0_ where items0_.student_id=?`

还有例外:

java.lang.StackOverflowError: null
    at org.h2.command.Parser.readIf(Parser.java:3524) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readWildcardOrSequenceValue(Parser.java:2933) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readTermObjectDot(Parser.java:2960) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readTerm(Parser.java:3095) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readFactor(Parser.java:2587) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readSum(Parser.java:2574) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readConcat(Parser.java:2544) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readCondition(Parser.java:2370) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readAnd(Parser.java:2342) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.readExpression(Parser.java:2334) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parseSelectSimpleSelectPart(Parser.java:2245) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parseSelectSimple(Parser.java:2277) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parseSelectSub(Parser.java:2133) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parseSelectUnion(Parser.java:1946) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parseSelect(Parser.java:1919) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parsePrepared(Parser.java:463) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parse(Parser.java:335) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.parse(Parser.java:307) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.command.Parser.prepareCommand(Parser.java:278) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.engine.Session.prepareLocal(Session.java:611) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.engine.Session.prepareCommand(Session.java:549) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1247) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76) ~[h2-1.4.197.jar:1.4.197]
    at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:304) ~[h2-1.4.197.jar:1.4.197]
    at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:318) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java) ~[HikariCP-2.7.9.jar:na]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:145) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:171) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:147) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.prepareQueryStatement(AbstractLoadPlanBasedLoader.java:226) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:190) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2223) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:565) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:247) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at java.util.Arrays.hashCode(Arrays.java:4146) ~[na:1.8.0_45]
    at java.util.Objects.hash(Objects.java:128) ~[na:1.8.0_45]
    at application.model.User.hashCode(User.java:127) ~[classes/:na]
    at java.util.Arrays.hashCode(Arrays.java:4146) ~[na:1.8.0_45]
    at java.util.Objects.hash(Objects.java:128) ~[na:1.8.0_45]
    at application.model.Item.hashCode(Item.java:54) ~[classes/:na]
    at java.util.HashMap.hash(HashMap.java:338) ~[na:1.8.0_45]
    at java.util.HashMap.put(HashMap.java:611) ~[na:1.8.0_45]
    at java.util.HashSet.add(HashSet.java:219) ~[na:1.8.0

我休息一下,得到的信息是这样的:

"error": "Internal Server Error",
"message": "HHH000142: Bytecode enhancement failed: application.model.User; nested exception is org.hibernate.HibernateException: HHH000142: Bytecode enhancement failed: application.model.User",

0 个答案:

没有答案