在Hiberate中避免循环引用

时间:2015-04-23 13:12:08

标签: java sql hibernate spring-mvc hibernate-mapping

我有三个域类.. Catalog,DeviceInventory,DeviceInventoryInfo .. Catalog类包含所有正在使用的设备,DeviceInventory类提供有关使用设备启动的成本的简要信息,DeviceInventoryInfo提供有关设备清单的详细说明。

设备清单是指来自Catalog类的名为modelId的字段。 Catalog类与DeviceInventory有一对多的关系。而DeviceInventoryInfo类引用具有一对一关系的设备库存ID的deviceInventory类。因此,每当我调用插入Web服务时,它都会显示grammaticalSQLException。这就是我在控制台中获得的内容

Hibernate: select inventoryr0_.device_inventory_id as device_i1_7_, inventoryr0_.device_model_id as device_m4_7_, inventoryr0_.hospital_id as hospital2_7_, inventoryr0_.manufacturer_id as manufact3_7_, inventoryr0_.physical_status as physical5_7_, inventoryr0_.device_status as device_s6_7_ from device_inventory_register inventoryr0_ where inventoryr0_.device_inventory_id=?
Hibernate: select catalog0_.device_model_id as device_m1_4_0_, catalog0_.baseline_cost as baseline2_4_0_, catalog0_.device_category_id as device_c3_4_0_, catalog0_.device_classification_id as device_c4_4_0_, catalog0_.device_description as device_d5_4_0_, catalog0_.device_code as device_c6_4_0_, catalog0_.device_name as device_n7_4_0_, catalog0_.device_type_id as device_18_4_0_, catalog0_.equivalent_model_name as equivale8_4_0_, catalog0_.hospital_id as hospital9_4_0_, catalog0_.life_expectancy as life_ex10_4_0_, catalog0_.manufacturer_id as manufac11_4_0_, catalog0_.meter_type as meter_t12_4_0_, catalog0_.device_model_num as device_13_4_0_, catalog0_.replacement_cost as replace14_4_0_, catalog0_.device_status as device_15_4_0_, catalog0_.device_subcategory_id as device_16_4_0_, catalog0_.support_status as support17_4_0_, catalog0_.useful_life as useful_19_4_0_, categoryma1_.device_category_id as device_c1_5_1_, categoryma1_.device_category_description as device_c2_5_1_, categoryma1_.device_category as device_c3_5_1_, categoryma1_.hospital_id as hospital4_5_1_, categoryma1_.status as status5_5_1_, devicetype2_.device_type_id as device_t1_11_2_, devicetype2_.device_type_description as device_t2_11_2_, devicetype2_.device_type as device_t3_11_2_, devicetype2_.hospital_id as hospital4_11_2_, subcategor3_.device_subcategory_id as device_s1_10_3_, subcategor3_.device_category_id as device_c2_10_3_, subcategor3_.hospital_id as hospital3_10_3_, subcategor3_.status as status4_10_3_, subcategor3_.device_subcategory_description as device_s5_10_3_, subcategor3_.device_subcategory as device_s6_10_3_ from device_catalog catalog0_ inner join device_category_master categoryma1_ on catalog0_.device_classification_id=categoryma1_.device_category_id inner join device_type_master devicetype2_ on catalog0_.device_type_id=devicetype2_.device_type_id inner join device_subcategory_master subcategor3_ on catalog0_.device_subcategory_id=subcategor3_.device_subcategory_id where catalog0_.device_model_id=?
 Hibernate: select inventoryr0_.s.no as s1_8_2_, inventoryr0_.date_of_acceptance as date_of_2_8_2_, inventoryr0_.account as account3_8_2_, inventoryr0_.baseline_cost as baseline4_8_2_, inventoryr0_.cost_to_date as cost_to_5_8_2_, inventoryr0_.device_inventory_id as device_i6_8_2_, inventoryr0_.stored_on_device as stored_o7_8_2_, inventoryr0_.hippa_data_id as hippa_da8_8_2_, inventoryr0_.date_of_installation as date_of_9_8_2_, inventoryr0_.date_of_launch as date_of10_8_2_, inventoryr0_.life_expectancy as life_ex11_8_2_, inventoryr0_.network_aware as network12_8_2_, inventoryr0_.network_connected as network13_8_2_, inventoryr0_.original_cost as origina14_8_2_, inventoryr0_.priority_id as priorit15_8_2_, inventoryr0_.date_of_purchase as date_of16_8_2_, inventoryr0_.date_of_receipt as date_of17_8_2_, inventoryr0_.replacement_cost as replace18_8_2_, inventoryr0_.risk_id as risk_id19_8_2_, inventoryr0_.skill_id as skill_i20_8_2_, inventoryr0_.transmitted_by_device as transmi21_8_2_, inventoryr0_.useful_life as useful_22_8_2_, inventoryr1_.device_inventory_id as device_i1_7_0_, inventoryr1_.device_model_id as device_m4_7_0_, inventoryr1_.hospital_id as hospital2_7_0_, inventoryr1_.manufacturer_id as manufact3_7_0_, inventoryr1_.physical_status as physical5_7_0_, inventoryr1_.device_status as device_s6_7_0_, catalog2_.device_model_id as device_m1_4_1_, catalog2_.baseline_cost as baseline2_4_1_, catalog2_.device_category_id as device_c3_4_1_, catalog2_.device_classification_id as device_c4_4_1_, catalog2_.device_description as device_d5_4_1_, catalog2_.device_code as device_c6_4_1_, catalog2_.device_name as device_n7_4_1_, catalog2_.device_type_id as device_18_4_1_, catalog2_.equivalent_model_name as equivale8_4_1_, catalog2_.hospital_id as hospital9_4_1_, catalog2_.life_expectancy as life_ex10_4_1_, catalog2_.manufacturer_id as manufac11_4_1_, catalog2_.meter_type as meter_t12_4_1_, catalog2_.device_model_num as device_13_4_1_, catalog2_.replacement_cost as replace14_4_1_, catalog2_.device_status as device_15_4_1_, catalog2_.device_subcategory_id as device_16_4_1_, catalog2_.support_status as support17_4_1_, catalog2_.useful_life as useful_19_4_1_ from device_inventory_register_info inventoryr0_ inner join device_inventory_register inventoryr1_ on inventoryr0_.device_inventory_id=inventoryr1_.device_inventory_id left outer join device_catalog catalog2_ on inventoryr1_.device_model_id=catalog2_.device_model_id where inventoryr0_.device_inventory_id=?

@Entity
@Table(name="device_catalog")
public class Catalog {




    private Integer modelId;
    private Integer hospitalId;
    private Integer manufacturerId;
    private String deviceCode;
    private String modelNumber;
    private String deviceName;
    private String description;
    private String equiModelName;
    private Integer classificationId;
    private Integer typeId;
    private Integer categoryId;
    private Integer subCategoryId;
    private String meterType;
    private Integer baselineCost;
    private Integer replacementCost;
    private Integer usefulLife;
    private Integer lifeExpectancy;
    private String supportStatus;
    public enum Status{Active, Inactive}

    @JsonBackReference
    private ManufacturerMaster master;

    private ClassificationMaster classificationMaster;

    private DeviceTypeMaster deviceTypeMaster;

    private CategoryMaster categoryMaster;

    private SubCategoryMaster subCategoryMaster;

    private Status status;

    @JsonManagedReference
    private Set<InventoryRegister> inventoryRegisters;

@Entity
@Table(name="device_inventory_register_info")
public class InventoryRegisterInfo {


    private Integer serialNum;
    private Integer deviceInventoryId;
    private String account;
    private Integer originalCost;
    private Integer replacementCost;
    private Integer costToDate;
    private Integer baselineCost;
    private Date purchaseDate;
    private Date receiptDate;
    private Date acceptanceDate;
    private Date installationDate;
    private Integer usefulLife;
    private Integer lifeExpectancy;
    private Date launchDate;
    private String networkAware;
    private String networkConnected;
    private String deviceStored;
    private String transmittedByDevice;
    private Integer skillId;
    private Integer priorityId;
    private Integer hippaId;
    private Integer riskId;

    private InventoryRegister inventoryRegister;

@Entity
@JsonAutoDetect
@Table(name="device_inventory_register")
public class InventoryRegister {

    private Integer deviceInventoryId;
    private Integer modelId;
    private Integer manufacturerId;
    private Integer hospitalId;
    private String physicalStatus;
    private Status status;

    @JsonBackReference
    private Catalog catalog;

    private InventoryRegisterInfo inventoryRegisterInfo;

那我怎么能阻止不必要的连接和循环引用呢?

1 个答案:

答案 0 :(得分:0)

在插入数据库之前,您是否尝试为每个实体设置循环引用为null?