Hibernate-无法延迟初始化角色集合:beans.LanguagePatient,无法初始化代理-没有会话

如何解决Hibernate-无法延迟初始化角色集合:beans.LanguagePatient,无法初始化代理-没有会话

您的json转换器尝试序列化整个实体,该实体包含使用每种语言的所有患者的列表。据我了解,json中的患者列表是不期望的。因此,您有三种选择(订购时我会考虑它们):

  • 在“语言”实体中删除对患者的映射。您需要从语言实体获得患者的准入吗?如果没有,请删除此映射。
  • 创建语言DTO,然后在退出tx层之前在其中传输数据。这样,无论谁调用该服务,都将永远不会收到LazyInitException。毫不奇怪:DTO字段总是设置得很热情。
  • 配置您的json转换器以不序列化患者字段。您尚未说出正在使用哪个json库。其中一些为您提供注释,以忽略某些字段(@JsonIgnore例如对于Jackson),其他则需要Java配置。

要应用第一个解决方案,请按以下方式更新这些文件:

Language.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 14, 2016 4:33:23 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="beans.Language" table="language" catalog="myglukose" optimistic-lock="version">
        <id name="idlanguage" type="java.lang.Integer">
            <column name="idlanguage" />
            <generator class="identity" />
        </id>
        <property name="language" type="string">
            <column name="language" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

语言.java

public class Language  implements java.io.Serializable {
    private Integer idlanguage;
    private String language;

    protected Language() {
    }


    public Language(String language) {
        this.language = language;
    }

    public Integer getIdlanguage() {
        return this.idlanguage;
    }

    protected void setIdlanguage(Integer idlanguage) {
        this.idlanguage = idlanguage;
    }
    public String getLanguage() {
        return this.language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
}

我已经将no- arg构造函数和setId方法更新为protected。您甚至可以将它们更新为private:只有休眠状态才能使用它们(并且它可以使用私有字段/方法)。

解决方法

我使用休眠来创建一个REST API。我创建了一种获取表中所有项目的方法。

public List<Language> getAllLanguages(Session session) {
        List<Language> languages=(List<Language>)session.createQuery("from Language").list();
        return languages;
}

这是我的Language.java

public class Language  implements java.io.Serializable {


     private Integer idlanguage;
     private String language;
     private Set<Patient> patients = new HashSet<Patient>(0);

    public Language() {
    }


    public Language(String language) {
        this.language = language;
    }
    public Language(String language,Set<Patient> patients) {
       this.language = language;
       this.patients = patients;
    }

    public Integer getIdlanguage() {
        return this.idlanguage;
    }

    public void setIdlanguage(Integer idlanguage) {
        this.idlanguage = idlanguage;
    }
    public String getLanguage() {
        return this.language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
    public Set<Patient> getPatients() {
        return this.patients;
    }

    public void setPatients(Set<Patient> patients) {
        this.patients = patients;
    }

}

这是我的Patient.java

// Generated Sep 14,2016 4:33:23 PM by Hibernate Tools 4.3.1


import beans.DiabetesType;
import beans.Illness;
import beans.Language;
import beans.Reminder;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Patient generated by hbm2java
 */
public class Patient  implements java.io.Serializable {

     private Integer idpatient;
     private DiabetesType diabetesType;
     private Language language;
     private String customId;
     private String diabetesOther;
     private String firstName;
     private String lastName;
     private String userName;
     private String password;
     private Date dateCreated;
     private Date lastUpdated;
     private Set<Illness> illnesses = new HashSet<Illness>(0);
     private Set<Reminder> reminders = new HashSet<Reminder>(0);


    public Patient() {
    }

    public Patient(Integer idpatient,String password) {
        this.idpatient = idpatient;
        this.password = password;
    }

    public Patient(DiabetesType diabetesType,Language language,String customId,String firstName,String userName,String password,Date lastUpdated) {
        this.diabetesType = diabetesType;
        this.language = language;
        this.customId = customId;
        this.firstName = firstName;
        this.userName = userName;
        this.password = password;
        this.lastUpdated = lastUpdated;
    }
    public Patient(DiabetesType diabetesType,String diabetesOther,String lastName,Date dateCreated,Date lastUpdated,Set<Illness> illnesses,Set<Reminder> reminders) {
       this.diabetesType = diabetesType;
       this.language = language;
       this.customId = customId;
       this.diabetesOther = diabetesOther;
       this.firstName = firstName;
       this.lastName = lastName;      
       this.userName = userName;
       this.password = password;
       this.dateCreated = dateCreated;
       this.lastUpdated = lastUpdated;
       this.illnesses = illnesses;
       this.reminders = reminders;
    }

    public Integer getIdpatient() {
        return this.idpatient;
    }

    public void setIdpatient(Integer idpatient) {
        this.idpatient = idpatient;
    }
    public DiabetesType getDiabetesType() {
        return this.diabetesType;
    }

    public void setDiabetesType(DiabetesType diabetesType) {
        this.diabetesType = diabetesType;
    }
    public Language getLanguage() {
        return this.language;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }
    public String getCustomId() {
        return this.customId;
    }

    public void setCustomId(String customId) {
        this.customId = customId;
    }
    public String getDiabetesOther() {
        return this.diabetesOther;
    }

    public void setDiabetesOther(String diabetesOther) {
        this.diabetesOther = diabetesOther;
    }
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public Date getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    public Date getLastUpdated() {
        return this.lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }
    public Set<Illness> getIllnesses() {
        return this.illnesses;
    }

    public void setIllnesses(Set<Illness> illnesses) {
        this.illnesses = illnesses;
    }
    public Set<Reminder> getReminders() {
        return this.reminders;
    }

    public void setReminders(Set<Reminder> reminders) {
        this.reminders = reminders;
    }
}

重要提示:Bean和映射是通过NetBeans从MySQL数据库反向工程的。patient调用时,不需要获取任何与之相关的数据getAllLangauges。我的language表格只有2列,idlanguagelanguagePatient表的前键为language table

在rest api中使用此方法之前,它没有任何异常即可完美运行。但是,当我在rest api中使用它时,它在那里造成了复杂性。

我在这里没有使用注释。我使用了休眠逆向工程向导来映射上述实体。这是我的其余api方法。

@Path("/language")
public class LanguageJSONService {

    @GET
    @Path("/getAllLanguages")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Language> getAllLanguages(){
        LanguageService languageService=new LanguageService();
        List<Language> list = languageService.getAllLanguages();
        return list;
    }
}

这就是我调用方法的方式,

Client client = ClientBuilder.newClient();
List<Language> list = client.target("http://localhost:8080/simple_rest/rest")
                .path("/language/getAllLanguages")
                .request(MediaType.APPLICATION_JSON)
                .get(new GenericType<List<Language>>() {
                });

for (int i = 0; i < list.size(); i++) {
      System.out.println("Id - " + list.get(i).getIdlanguage() + " Language - " + list.get(i).getLanguage());
}

当我调用该方法时,

failed to lazily initialize a collection of role: beans.Language.patients,could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->beans.Language["patients"])

发生了。

有趣的是,如果我没有关闭会话,那么我将得到如下所示的输出,这完全是另外一回事,似乎它正在尝试显示其外键表及其外键表,等等。

    [{"idlanguage":1,"language":"English","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":[{"idpatient":1,"diabetesType":

对这个问题有什么想法吗?

更新资料

我的配置文件

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="show_sql">true</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/*****</property>
    <property name="hibernate.connection.username">*****</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">3000</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">300</property>
    <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    <property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
    <property name="hibernate.connection.password">************</property>
    <mapping resource="beans/Reminder.hbm.xml"/>
    <mapping resource="beans/Food.hbm.xml"/>
    <mapping resource="beans/Patient.hbm.xml"/>
    <mapping resource="beans/Illness.hbm.xml"/>
    <mapping resource="beans/Language.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Language.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 14,2016 4:33:23 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="beans.Language" table="language" catalog="myglukose" optimistic-lock="version">
        <id name="idlanguage" type="java.lang.Integer">
            <column name="idlanguage" />
            <generator class="identity" />
        </id>
        <property name="language" type="string">
            <column name="language" length="45" not-null="true" />
        </property>
        <set name="patients" table="patient" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="language_idlanguage" not-null="true" />
            </key>
            <one-to-many class="beans.Patient" />
        </set>
    </class>
</hibernate-mapping>

这是我的患者映射文件,

Patient.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 14,2016 4:33:23 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="beans.Patient" table="patient" catalog="myglukose" optimistic-lock="version">
        <id name="idpatient" type="java.lang.Integer">
            <column name="idpatient" />
            <generator class="identity" />
        </id>
        <many-to-one name="diabetesType" class="beans.DiabetesType" fetch="select">
            <column name="diabetes_type_iddiabetes_type" not-null="true" />
        </many-to-one>
        <many-to-one name="language" class="beans.Language" fetch="select">
            <column name="language_idlanguage" not-null="true" />
        </many-to-one>
        <property name="customId" type="string">
            <column name="custom_id" length="45" not-null="true" />
        </property>
        <property name="diabetesOther" type="string">
            <column name="diabetes_other" length="45" />
        </property>
        <property name="firstName" type="string">
            <column name="first_name" length="100" not-null="true" />
        </property>
        <property name="lastName" type="string">
            <column name="last_name" length="100" />
        </property>        
        <property name="userName" type="string">
            <column name="user_name" length="45" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" length="45" not-null="true" />
        </property>
        <property name="dateCreated" type="timestamp">
            <column name="date_created" length="19" />
        </property>
        <property name="lastUpdated" type="timestamp">
            <column name="last_updated" length="19" not-null="true">
                <comment>Stores the basic information of the patient</comment>
            </column>
        </property>
        <set name="illnesses" table="illness" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="patient_idpatient" not-null="true" />
            </key>
            <one-to-many class="beans.Illness" />
        </set>
        <set name="reminders" table="reminder" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="patient_idpatient" not-null="true" />
            </key>
            <one-to-many class="beans.Reminder" />
        </set>
    </class>
</hibernate-mapping>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res