Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
F'Nog
Jul 21, 2001

ColdPie posted:

Reflection! And Generics!

I'd like to get the type parameter, as a Class, Type, or even just the canonical name String, used in a generic class. For example, I'd like to get the Class object for java.lang.String from a Vector<String> object, or a Class object for java.lang.Integer from a Vector<Integer> object.

I found the Class.getTypeParameters() method, but the best I can get out of that is a java.lang.Object Type object.

This isn't crucial to my project, so if it's impossible it's impossible. It would really simplify maintenance and documentation if there is a way to get it, though.

Any ideas?

Just to double check what you're meaning, given any Vector<?> you want to retrieve the class of ? at runtime? Sorry to say but you can't. Java uses type erasure for generics which means at runtime all the type information is gone.

Adbot
ADBOT LOVES YOU

F'Nog
Jul 21, 2001

ColdPie posted:

Lame. Then what's the point of Class.getTypeParameters()?

It just gives you a nice representation of the classes generic declaration.

i.e.

code:
    class Farts<K extends AbstractList>
    {
        private K what;

        public Farts(K what)
        {
            this.what = what;
        }
    }
If I pull off the type parameters you get a name of 'K' and a bound of AbstractList because I'm contraining the super classes/interfaces. Generic declaration then basically drills down in case you've got a class with generics which then itself is generic.

F'Nog
Jul 21, 2001

TRex EaterofCars posted:

I'm curious what that client's excuse for sticking with Java 1.3 is. Did they use some Sun internal API that got removed?
I've worked with a bank's gateway package that threw a runtime exception if it detected a 1.5 VM. People do the wackiest things, I can't really think of anything that's actually been removed rather than deprecated.

F'Nog
Jul 21, 2001

FearIt posted:

I'm typing 'objectsName.' in my IDE (eclipse) and when the list of methods comes up none of them are clone(). The object is a class I defined myself, would I need it to implement something for .clone() to work? Or alter my class in someway?

clone() is protected in Object, you need to open it up yourself in the class you want to clone.

F'Nog
Jul 21, 2001

Mill Town posted:

Sounds cool, but I see it takes SQL commands as raw text. Is there a sanitize function available? I can't find one by searching the site or Google. Thanks!
It supplies a JDBC driver doesn't it support PreparedStatements? We use HSQLDB (H2 is another option, they both came about when Hypersonic forked) for the couple of instances we need an embedded database. Derby is an optional bundle with the JDK not the JRE so it's presence in an environment is something you'll have to make certain of anyway. Bundling a DB with the JDK is retarded anyway, it's too transient. I wonder if we'll get Glassfish when version 7 comes out.

F'Nog
Jul 21, 2001

epswing posted:

In an RDBMS, it's typical to normalize something like UserType (or PaymentMethod) into into its own table like 1=admin, 2=viewer (or 1=cash, 2=cheque, 3=creditcard), and other tables reference them by id. Such tables won't change, typically. In Java code, it makes sense to think of these as enums, so a Payment class would have a PaymentMethod field, where PaymentMethod is an enum: PaymentMethod.CASH, PaymentMethod.CHEQUE, etc.

How can I use Hibernate to represent this common use case?

The following links are what google has to say about the issue, but these solutions seem overly complex and/or are several years old.

http://www.hibernate.org/265.html
http://www.hibernate.org/265.html
http://appfuse.org/display/APF/Java+5+Enums+Persistence+with+Hibernate
http://www.hibernate.org/272.html
http://snipplr.com/view/5379/java-5-enum-hibernate-mapping/
http://hibernate.org/203.html

Does anyone have experience with this, and/or can vouch for one of the linked solutions?

Enlighten me!

(E: Don't get me wrong, I'm usually all for just trying things out, but the linked solutions look old, long and complicated, and this very well may be a case of someone here saying "oh just do X, it works great".)

We do it that way, use a UserType to handle the mapping, you should be able to do it pretty generically and handle just about every type you'll encounter (they usually just pass a number id through to hibernate on persistence, the other parameters of the enum are just for pretty printing and stuff). You can then use the enum object itself in HQL/Criteria/setters/etc.

F'Nog
Jul 21, 2001

epswing posted:

I'm finding your reply somewhat vague. What do you mean by "that way"? Can you give an example?

All the samples you posted use a UserType to control the marshalling between the enum and the database, the only real differences between them are personal preference. That's pretty much how everyone does it. The cleanest example is probably http://appfuse.org/display/APF/Java+5+Enums+Persistence+with+Hibernate and you can take it a bit further and put and interface over your enums so you don't have to always specify the identifierMethod or the valueOfMethod.

F'Nog
Jul 21, 2001

8ender posted:

Does anyone here have any experience with the various persistence APIs? I'm trying to decide which ones to evaluate right now and its a maze of wierdness when it comes to who owns what and which is a standard or not.

From what I can gather Oracle bought TOP and made Toplink. Later a cut down version of Toplink was combined with some Hibernate know how to create the JPA in EJB3. Then Oracle open sourced Toplink and it became Eclipselink which is going to be JPA in JavaEE6. Then there is Hibernate just hanging out doing its thing.

We're looking at evaluating EJB3, Toplink/Eclipselink and Hibernate right now. Any others anyone can add the list? Is it worthless to look at the JPA in EJB3 with Eclipselink being the standard for JavaEE6?

The JPA is just the set of interfaces that live in the runtime and all the providers your listed implement them. I've only every used Hibernate, there isn't anything it doesn't do that I've needed. It just plops in and works. It's also got a pretty big userbase for reference material.

Adbot
ADBOT LOVES YOU

F'Nog
Jul 21, 2001

8ender posted:

Sorry, I guess I meant the "Toplink Essentials/Hibernate mashup" that is bundled with the EJB 3.0 spec. How would you compare Hibernate to EclipseLink? In my case we have an existing data model and we'll be trying to match to our database.

I've never used Eclipselink in a project before so I'm going on what I've read, Eclipselink is apparently better at it's second-level caching and probably has an out of the box cluster aware caching ability (Hibernate requires you to pick a caching provider first), and it's also quite awesome when running against the latest Oracle instances which I guess is to be expected. It's also got a bunch of stuff embedded in it that Hibernate doesn't have like JAXB support which I'm not too confident about because the two just don't fit together. You need seperate models to do both JPA and JAXB annotations as using a feature of one always ends up meaning you can't do something with the other.

Personally for your situation I'd just go with Hibernate, annotate your domain model against the JPA as much as you can without straying into Hibernate's extensions and that way if Hibernate doesn't fit your needs substituting Eclipselink in won't be hard.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply