Wednesday, December 30, 2009

More compact @PersistenceCapable declarations

When I started writing I said I was going to treat JDO and JPA equally in these posts, but I've just learned something so earth-shatteringly helpful that happens to be specific to JDO. What would you have me do? Should I withhold valuable information just because it doesn't have a corresponding representation in JPA? That hardly seems right. How about a compromise: I'm going to share the JDO nugget and I promise to make it up to the JPA users at some point in the future. Sound reasonable? We have a deal? Ok, good. Now hold on to your hats, here we go:

I've always written PersistenceCapable declarations like this:

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Foo {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    // ...
}

Kinda verbose, right? It turns out that if you omit 'identityType' DataNucleus will still do the right thing so long as you've set your PrimaryKey up properly:

@PersistenceCapable
public class Foo {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    // ...
}

I just saved you 41 characters. Happy new year everyone!

8 comments:

  1. 忙碌的一天終於過了,來看看文章轉換心情,也幫你加個油哦~..................................................

    ReplyDelete
  2. I personally would say that

    @Persistent
    String name;

    is too verbose, and that docs should mention that there is a default persistent flag for each java type, and basic things like String don't need that.

    Just saved you 11 more characters :-P

    ReplyDelete
  3. We had this debate internally before launch and decided that it was simpler to recommend @Persistent on every field than to ask users to learn which types were persistent by default and which were not. Given the amount of confusion that the default fetch group caused before we expanded it to include all app engine native datastore types, I think we made the right call. In a perfect world everyone would have the time and inclination to read the GAE docs AND the DataNucleus docs before they started coding, but in practice I don't see it happening.

    ReplyDelete
  4. These terms are used to describe the isolation of the primary and secondary windings or isolation between the two. A shielded transformer is designed with a metallic shield between the primary and secondary windings to attenuate transient noise. Properties in noida We specialize in Noida and Greater Noida in all sorts of real estate property. Our impeccable services and honest policy makes us the best to deal in real estate in & around Noida.

    ReplyDelete
  5. Ramprastha SKYZ Gurgaon sector 37D on Dwarka Express Way is Latest residential Project in Gurgaon having option 1bhk/2bhk/3bhk apartment with study room. Contact Homeseek Realtors for Ramprastha city as SKYZ.

    ReplyDelete
  6. Gurgaon property asAbw verona hills on NH 8 sector-76 gurgaon is 2bhk/3bhk apartment having all morden facility. Homeseek Realtors for best deal Abw Verona Hills .

    ReplyDelete
  7. Gurgaon is booming sector in all real estate sectors. Prices rise high day by day..

    ReplyDelete
  8. We have 2/3/4 BHK ready Property in Noida with the best price. Contact us for more details or visit www.renrealty.in Call me @: 91-9999598214, 91-9999598218, 91-9582222904
    Greater Noida Property then this will help you

    ReplyDelete