子实体计入hibernate

时间:2017-09-04 15:43:54

标签: java database oracle hibernate

我正在开发一个项目,其中两个实体具有单向@OneToMany映射。 当我尝试使用此查询加载特定父ID的所有子实体时 -

select p.childEntities from Parent p where p.id =:parentId

工作正常。还要注意,我在这里使用hibernate api进行分页,所以我只得到10个,25个......记录。 在许多方面,我只需要实体数量。 现在我试图只使用此查询加载所有子实体的计数 -

select count(p.childEntities) from Parent p where p.id =:parentId

因ORACLE错误代码失败 - ORA-00936:缺少表达式

我的情况(此项目的权限较低) - 我无法将实体映射更改为双向。并且没有使用原生SQL。 另外我认为使用 -

获取所有列表

" select p.childEntities from Parent p where p.id =:parentId "

然后只是获得计数的大小()是成本高昂的。

项目中的映射一瞥 - 父类 -

 @Entity
@Table(name = "PARENT")
public class Parent implements Serializable{

    private static final long             serialVersionUID     = 2232715856164345328L;

    private Long   id;

    private String first;

    private String second;

    private String third;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_id")
    private List<Child> childEntities;


    public Long getId() {
        return id;
    }

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

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public String getThird() {
        return third;
    }

    public void setThird(String third) {
        this.third = third;
    }

    public List<Child> getChildEntities() {
        return childEntities;
    }

    public void setChildEntities(List<Child> childEntities) {
        this.childEntities = childEntities;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

和儿童班 -

@Entity
@Table(name = "Child")
public class Child {

    private Long   id;

    private Integer number;

    private String prop1;

    private String prop2;

    public Long getId() {
        return id;
    }

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

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getProp1() {
        return prop1;
    }

    public void setProp1(String prop1) {
        this.prop1 = prop1;
    }

    public String getProp2() {
        return prop2;
    }

    public void setProp2(String prop2) {
        this.prop2 = prop2;
    }
}

我还有哪些选择?

1 个答案:

答案 0 :(得分:1)

您需要了解连接(这也是您在SQL中使用的,BTW):

$folderPath = "C:\Users\user\folder\*" # multi-folders: "C:\fso1*", "C:\fso2*"
$fileType = "*.doc"                    # *.doc will take all .doc* files

$textToReplace = @{
# "TextToFind" = "TextToReplaceWith"
"This1" = "That1"
"This2" = "That2"
"This3" = "That3"
}

$word = New-Object -ComObject Word.Application
$word.Visible = $false

$storyTypes = [Microsoft.Office.Interop.Word.WdStoryType]
#Val, Name
#  1, wdMainTextStory
#  2, wdFootnotesStory
#  3, wdEndnotesStory
#  4, wdCommentsStory
#  5, wdTextFrameStory
#  6, wdEvenPagesHeaderStory
#  7, wdPrimaryHeaderStory
#  8, wdEvenPagesFooterStory
#  9, wdPrimaryFooterStory
# 10, wdFirstPageHeaderStory
# 11, wdFirstPageFooterStory
# 12, wdFootnoteSeparatorStory
# 13, wdFootnoteContinuationSeparatorStory
# 14, wdFootnoteContinuationNoticeStory
# 15, wdEndnoteSeparatorStory
# 16, wdEndnoteContinuationSeparatorStory
# 17, wdEndnoteContinuationNoticeStory

Function findAndReplace($objFind, $FindText, $ReplaceWith) {
  #simple Find and Replace to execute on a Find object
  $matchCase = $true
  $matchWholeWord = $true
  $matchWildcards = $false
  $matchSoundsLike = $false
  $matchAllWordForms = $false
  $forward = $true
  $findWrap = [Microsoft.Office.Interop.Word.WdReplace]::wdReplaceAll
  $format = $false
  $replace = [Microsoft.Office.Interop.Word.WdFindWrap]::wdFindContinue

  $objFind.Execute($FindText, $matchCase, $matchWholeWord, $matchWildCards, $matchSoundsLike, $matchAllWordForms, \`
           $forward, $findWrap, $format, $ReplaceWith, $replace) > $null
}

Function findAndReplaceAll($objFind, $FindText, $ReplaceWith) {
    findAndReplace $objFind $FindText $ReplaceWith
    While ($objFind.Found) {
        findAndReplace $objFind $FindText $ReplaceWith
    }
}

Function findAndReplaceMultiple($objFind, $lookupTable) {
  #apply multiple Find and Replace on the same Find object
  $lookupTable.GetEnumerator() | ForEach-Object {
    findAndReplaceAll $objFind $_.Key $_.Value
  }
}

Function findAndReplaceMultipleWholeDoc($Document, $lookupTable) {
  ForEach ($storyRge in $Document.StoryRanges) {
    #Loop through each StoryRange
    Do {
      findAndReplaceMultiple $storyRge.Find $lookupTable
      #check if the StoryRange has shapes (we check only StoryTypes 6 to 11, basically Headers and Footers)
      # as the Shapes inside the wdMainTextStory will be checked
      # see http://wordmvp.com/FAQs/Customization/ReplaceAnywhere.htm
      # and http://gregmaxey.com/using_a_macro_to_replace_text_wherever_it_appears_in_a_document.html
      If (($storyRge.StoryType -ge $storyTypes::wdEvenPagesHeaderStory) -and \`
        ($storyRge.StoryType -le $storyTypes::wdFirstPageFooterStory)) {
        If ($storyRge.ShapeRange.Count) { #non-zero is True
          ForEach ($shp in $storyRge.ShapeRange) {
            If ($shp.TextFrame.HasText) { #non-zero is True, in case of text .HasText = -1
              findAndReplaceMultiple $shp.TextFrame.TextRange.Find $lookupTable
            }
          }
        }
      }
      #check for linked Ranges
      $storyRge = $storyRge.NextStoryRange
    } Until (!$storyRge) #non-null is True

  }
}

Function processDoc {
  $doc = $word.Documents.Open($_.FullName)
  # The "VBA trickey" translated to PowerShell...
  $junk = $doc.Sections.Item(1).Headers.Item(1).Range.StoryType
  #... but not working
  findAndReplaceMultipleWholeDoc $doc $textToReplace
  $doc.Close([ref]$true)
}

$sw = [Diagnostics.Stopwatch]::StartNew()
$countf = 0
Get-ChildItem -Path $folderPath -Recurse -Filter $fileType | ForEach-Object { 
  Write-Host "Processing \`"$($_.Name)\`"..."
  processDoc
  $countf++
}
$sw.Stop()
$elapsed = $sw.Elapsed.toString()
Write-Host "Done. $countf files processed in $elapsed"

$word.Quit()
$word = $null
[gc]::collect() 
[gc]::WaitForPendingFinalizers()