Time |
S |
Nick |
Message |
00:47 |
|
|
SoniEx2|2 joined ##javaee |
03:46 |
|
|
SoniEx2|2 joined ##javaee |
04:46 |
|
|
SoniEx2|2 joined ##javaee |
06:42 |
|
|
Quest joined ##javaee |
08:27 |
|
|
Quest joined ##javaee |
10:05 |
|
|
sfisque joined ##javaee |
12:46 |
|
Naros |
morning sfisque |
13:20 |
|
|
Naros joined ##javaee |
13:21 |
|
Naros |
morning all |
13:29 |
|
pdurbin |
mornin' |
13:31 |
|
Naros |
pdurbin: you have any experience with @EntityListeners? |
13:34 |
|
pdurbin |
nope |
13:35 |
|
pdurbin |
and `ack EntityListeners` returns nothing from our codebase |
13:36 |
|
Naros |
How about using things such as @PrePersist or @PostLoad or @PreUpdate, etc. |
13:37 |
|
Naros |
Essentially, @EntityListener and @EntityListeners allow you to define a callback class rather than muck your entities with JPA event callback methods. |
13:37 |
|
pdurbin |
hmm. ok. interesting |
13:38 |
|
Naros |
Anyway, looks like JPA instantiates these listeners on a per-entity basis. |
13:38 |
|
Naros |
which seems silly if they are suppose to be stateless. |
13:39 |
|
Naros |
I'd argue a cleaner approach would be to allocate one of each class and maintain some relationship between @Entity & @EntityListener |
13:40 |
|
Naros |
but can't find a way to make JPA do it :( |
13:44 |
|
pdurbin |
:( |
13:46 |
|
|
SoniEx2 joined ##javaee |
14:55 |
|
whartung |
Naros: you can also put the entity listener methods in the actual entity, then this whole discussion is effectively moot. |
14:56 |
|
whartung |
the problem with making the listeners singletons, is then the framework writers are compelled to add Yet Another Lifecycle to start, stop, and tear down the Entity Listener, since the contract of a singleton is different from the contract of an instance variable. |
14:57 |
|
whartung |
if you want, you could always have a singleton service that the EntityListener leverages, but you'll still get an EL instance each time, however light weight it is. |
14:58 |
|
whartung |
But ELs tend to be little more than blobs of code, so the object over head is real small and, yea, they do add to GC pressure, marginally, but that's about as extreme a performance hit they add to the system. |
15:03 |
|
pdurbin |
the term EL is overloaded in Java EE :) |
15:03 |
|
whartung |
yes, but not in the context of this note. |
15:14 |
|
Naros |
whartung: understood. looks like in Hibernate 4.3, they instantiate each listener once by type where as in 4.2, they're instantiated by entity |
15:14 |
|
whartung |
ok |
15:14 |
|
Naros |
but let me ask you this |
15:15 |
|
Naros |
Lets say some entity has an embedded class |
15:15 |
|
|
Quest joined ##javaee |
15:15 |
|
whartung |
biggest complain about ELs is that you can't actually do any JPA stuff in them... |
15:15 |
|
whartung |
you mean for the key? |
15:15 |
|
Naros |
No, not the key but actual repeatable column data. |
15:15 |
|
whartung |
ok |
15:15 |
|
Quest |
now we have two naros :) |
15:16 |
|
Naros |
Like a majority of entities perhaps have a set of 5 or 7 columns. |
15:16 |
|
whartung |
sure |
15:16 |
|
Naros |
I could allocate this embeddable in the private variable declaration of the entity. |
15:16 |
|
Naros |
but in the case of fetches, that is an unnecessary allocation since the ORM will do their own. |
15:16 |
|
whartung |
yes |
15:16 |
|
Naros |
so what I am trying to understand and follow are best practices for this use case. |
15:17 |
|
Naros |
Are those embeddables generally put off on the user of the persistence API to new when necessary during persists? |
15:17 |
|
whartung |
so you have |
15:17 |
|
whartung |
MyClass |
15:17 |
|
whartung |
which has |
15:17 |
|
whartung |
MyClass.get/setMyEmbedded |
15:17 |
|
Naros |
Right |
15:17 |
|
whartung |
right? |
15:18 |
|
Naros |
one way to avoid the unnecessary alloc is to have the get check if the field is null and if so, allocate and return the reference. that works for hibernate ofc, but maybe not another JPA provider. |
15:18 |
|
whartung |
so, then if you fetch a MyClass, the JPA should create a MyEmbedded as necessary, so myClass.getEmbedded will work |
15:18 |
|
whartung |
but if you do |
15:18 |
|
whartung |
myClass = new MyClass(); |
15:19 |
|
whartung |
then, yes, it's up to you to create the MyEmbedded instance |
15:19 |
|
whartung |
and doing it in the getter is fine |
15:19 |
|
whartung |
if (myEmbedded == null) { myEmbedded = new MyEmbedded(); } return myEmbedded |
15:19 |
|
Naros |
Which do you believe is generally more acceptable, realizing there are many ways to cook this goose? |
15:20 |
|
whartung |
we use that patterns for collections |
15:20 |
|
whartung |
but I have never used embedded classes |
15:20 |
|
Naros |
the getter pattern? |
15:20 |
|
whartung |
JPA will NOT use your getter, since by default JPA is FIELD based. It'll never even see the getter |
15:20 |
|
whartung |
but the JPA will (should) create the instance on the fly for you anyway |
15:21 |
|
Naros |
Seems it does if annotations are on getters and not fields. |
15:21 |
|
whartung |
yea, you can change it |
15:21 |
|
whartung |
we don' |
15:21 |
|
whartung |
we just use the fields |
15:21 |
|
whartung |
and the get/setters for our code |
15:21 |
|
Naros |
Gotcha. |
15:22 |
|
Naros |
I was using some ELs to do some of this logic since they fire before the JPA provider's validation code fires but that feels overkill. |
15:22 |
|
Naros |
But wasn't sure if doing some null check in getters was taboo |
15:22 |
|
whartung |
shouldn't have to use the listeners at all for this |
15:23 |
|
Naros |
Aye, I feel the same. Listeners didn't feel right for this. |
15:23 |
|
whartung |
myClass = em.find(1); should Just Work like you think |
15:23 |
|
Naros |
Although some believe that is the perfect place to maintain date created and date last updated logic tho |
15:24 |
|
whartung |
and myClass.setEmbedded(new MyEmbedded()); em.persist(myClass) should just work as well |
15:24 |
|
whartung |
oh, sure listeners are fine for that |
15:24 |
|
Naros |
Aye, so long as I set it with a new, sure. but if I dont call the setter, it fails with error. |
15:24 |
|
whartung |
if the myEmbedded instance variable is null you get an error? |
15:25 |
|
Naros |
Aye, these two embeddables are AuditColumns which have the date craeted & last updated and DisabledColumns which handles entity disabled flags & disabled date logic. |
15:25 |
|
whartung |
in the MyClass? |
15:25 |
|
whartung |
ok |
15:25 |
|
whartung |
are they required? |
15:25 |
|
Naros |
Yes because for disabled flag the column declaration is |
15:25 |
|
Naros |
@Column(name="DISABLED_FLAG",nullable=false) |
15:25 |
|
crimsonfubot |
Naros: Error: "Column(name="DISABLED_FLAG",nullable=false)" is not a valid command. |
15:25 |
|
Naros |
grr |
15:25 |
|
Naros |
..@Column(name="DISABLED_FLAG",nullable=false) |
15:25 |
|
Naros |
yes, one is required in the column definition |
15:26 |
|
whartung |
so you have an embeddable with a required field within it |
15:26 |
|
Naros |
by default i wasn't it always to not be disabled and the user not have to worry with that |
15:26 |
|
whartung |
then have the instance be null should give you an error, no? |
15:26 |
|
Naros |
Yep |
15:26 |
|
Naros |
something about non-transient non-null value |
15:26 |
|
whartung |
huh |
15:26 |
|
whartung |
yea, you're getting in to uncharted territory for me here, since I've never used them |
15:27 |
|
whartung |
butI think you use case is a prefact use for these |
15:27 |
|
Naros |
Basically because the embeddable has a nullable=false definition on a column, the embeddable itself can't be null. |
15:27 |
|
Naros |
one i allocated the embeddable and set it, it persisted just fine. |
15:27 |
|
Naros |
*once |
15:27 |
|
whartung |
yea, semantically that makes sense, whether JPA groks that or relies on the DB to punt kittens when it happens, I can't say. |
15:28 |
|
Naros |
:P |
15:28 |
|
Naros |
i prefer punting gnomes myself |
15:29 |
|
whartung |
yea, not a fan of kitten abuse |
15:30 |
|
whartung |
here's your morning kitten productivity boost |
15:30 |
|
whartung |
https://i.chzbgr.com/maxW500/7726494976/h84FCA79A/ |
15:31 |
|
whartung |
See...http://www.washingtonpost.com/blogs/wonkblog/wp/2012/10/01/want-to-increase-your-productivity-study-says-look-at-this-adorable-kitten/ |
15:31 |
|
whartung |
SCIENCE! Don't be a denier... |
15:32 |
|
Naros |
ROFL -- I knew there was a reason I owned two adorable cats. |
15:32 |
|
Naros |
Never mind the fact that one weighs 17lbs and the other 8lbs. |
15:33 |
|
whartung |
yea we have two as well |
15:36 |
|
|
sfisque joined ##javaee |
15:47 |
|
|
Quest left ##javaee |
15:52 |
|
|
kobain joined ##javaee |
20:37 |
|
|
joshua_jandyco joined ##javaee |
20:48 |
|
|
joshua_jandyco joined ##javaee |
21:31 |
|
|
jxriddle joined ##javaee |
21:33 |
|
|
jxriddle left ##javaee |
21:46 |
|
|
SoniEx2 joined ##javaee |
22:05 |
|
|
crimsonfubot joined ##javaee |
22:05 |
|
|
kobain_ joined ##javaee |
22:12 |
|
|
kobain joined ##javaee |
22:29 |
|
|
kobain_ joined ##javaee |
23:29 |
|
|
sfisque joined ##javaee |
23:35 |
|
|
sfisque1 joined ##javaee |