{"action":"created","issue":{"url":"https://api.github.com/repos/timolson/cointrader/issues/114","labels_url":"https://api.github.com/repos/timolson/cointrader/issues/114/labels{/name}","comments_url":"https://api.github.com/repos/timolson/cointrader/issues/114/comments","events_url":"https://api.github.com/repos/timolson/cointrader/issues/114/events","html_url":"https://github.com/timolson/cointrader/issues/114","id":90229390,"number":114,"title":"EntityNotFoundException when merging Position with cascades","user":{"login":"douggie","id":4409194,"avatar_url":"https://avatars.githubusercontent.com/u/4409194?v=3","gravatar_id":"","url":"https://api.github.com/users/douggie","html_url":"https://github.com/douggie","followers_url":"https://api.github.com/users/douggie/followers","following_url":"https://api.github.com/users/douggie/following{/other_user}","gists_url":"https://api.github.com/users/douggie/gists{/gist_id}","starred_url":"https://api.github.com/users/douggie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/douggie/subscriptions","organizations_url":"https://api.github.com/users/douggie/orgs","repos_url":"https://api.github.com/users/douggie/repos","events_url":"https://api.github.com/users/douggie/events{/privacy}","received_events_url":"https://api.github.com/users/douggie/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-22T22:27:43Z","updated_at":"2015-06-23T05:06:01Z","closed_at":null,"body":"When trying to persit a postion with the folloiwng annotations using multiple threads the following execption occurs, dispite the the fill being persited.\r\n\r\nFill\r\n```java\r\n    public @ManyToOne(optional = true, cascade = { CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH })\r\n    @JoinColumn(name = \"position\")\r\n    Position getPosition() {\r\n        return position;\r\n    }\r\n```\r\n\r\nPosition\r\n```java\r\n    @OneToMany(fetch = FetchType.EAGER, mappedBy = \"position\", cascade = { CascadeType.MERGE, CascadeType.REFRESH })\r\n    @NotFound(action = NotFoundAction.IGNORE)\r\n    public Collection<Fill> getFills() {\r\n        if (fills == null)\r\n            fills = new ConcurrentLinkedQueue<Fill>();\r\n        return fills;\r\n\r\n    }\r\n```\r\n\r\nPosition constructor\r\n\r\n```java\r\npublic Position(Fill fill) {\r\n\r\n        this.addFill(fill);\r\n        this.exchange = fill.getMarket().getExchange();\r\n        this.market = fill.getMarket();\r\n        this.volume = fill.getVolume();\r\n        this.volumeCount = volume.toBasis(market.getVolumeBasis(), Remainder.ROUND_EVEN).getCount();\r\n        this.longVolume = volume.isPositive() ? volume : this.longVolume;\r\n        this.longVolumeCount = volume.isPositive() ? volume.toBasis(market.getVolumeBasis(), Remainder.ROUND_EVEN).getCount() : this.longVolumeCount;\r\n        this.shortVolume = volume.isNegative() ? volume : this.shortVolume;\r\n        this.shortVolumeCount = volume.isNegative() ? volume.toBasis(market.getVolumeBasis(), Remainder.ROUND_EVEN).getCount() : this.shortVolumeCount;\r\n        this.longAvgPrice = volume.isPositive() ? fill.getPrice() : this.longAvgPrice;\r\n        this.shortAvgPrice = volume.isNegative() ? fill.getPrice() : this.shortAvgPrice;\r\n        this.shortAvgStopPrice = fill.getStopPrice() != null && fill.isShort() ? fill.getStopPrice() : DecimalAmount.ZERO;\r\n        this.longAvgStopPrice = fill.getStopPrice() != null && fill.isLong() ? fill.getStopPrice() : DecimalAmount.ZERO;\r\n        this.asset = fill.getMarket().getListing().getBase();\r\n\r\n        this.portfolio = fill.getPortfolio();\r\n        fill.setPosition(this);\r\n        fill.getPortfolio().addPosition(this);\r\n\r\n    }\r\n```\r\n\r\n```\r\njavax.persistence.EntityNotFoundException: Unable to find org.cryptocoinpartners.schema.Fill with id 9afc29d3-df3d-490f-8545-54d228bf1dce\r\n```\r\nThoughts\r\n1. As adding the fill to the position within the position constructor rather than the fill constructor is confusing JPA\r\n2. another thread is removing the fill from the position before it has been persisted.\r\n"},"comment":{"url":"https://api.github.com/repos/timolson/cointrader/issues/comments/114359361","html_url":"https://github.com/timolson/cointrader/issues/114#issuecomment-114359361","issue_url":"https://api.github.com/repos/timolson/cointrader/issues/114","id":114359361,"user":{"login":"timolson","id":1697115,"avatar_url":"https://avatars.githubusercontent.com/u/1697115?v=3","gravatar_id":"","url":"https://api.github.com/users/timolson","html_url":"https://github.com/timolson","followers_url":"https://api.github.com/users/timolson/followers","following_url":"https://api.github.com/users/timolson/following{/other_user}","gists_url":"https://api.github.com/users/timolson/gists{/gist_id}","starred_url":"https://api.github.com/users/timolson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timolson/subscriptions","organizations_url":"https://api.github.com/users/timolson/orgs","repos_url":"https://api.github.com/users/timolson/repos","events_url":"https://api.github.com/users/timolson/events{/privacy}","received_events_url":"https://api.github.com/users/timolson/received_events","type":"User","site_admin":false},"created_at":"2015-06-23T05:06:01Z","updated_at":"2015-06-23T05:06:01Z","body":"I don't know much about JPA cascading, but if it does the obvious thing, then you should only have to set   the relationship in one place.  Also, what does the call to `merge()` look like?  You just use `PersistUtil`?\r\n\r\nWe \"should\" be JPA threadsafe from earlier work."}}