14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Java annotation 169<br />

programmatically (with API calls). The following, for example, is an annotated EJB 3.0 data class:<br />

@Entity // Declares this an<br />

entity bean<br />

@Table(name = "people") // Maps the bean to<br />

SQL table "people"<br />

class Person implements Serializable {<br />

@Id // Map this to the<br />

primary key column.<br />

@GeneratedValue(strategy = GenerationType.AUTO) // Database will<br />

generate new primary keys, not us.<br />

private Integer id;<br />

@Column(length = 32) // Truncate column<br />

values to 32 characters.<br />

}<br />

private String name;<br />

public Integer getId() {<br />

}<br />

return id;<br />

public void setId(Integer id) {<br />

}<br />

this.id = id;<br />

public String getName() {<br />

}<br />

return name;<br />

public void setName(String name) {<br />

}<br />

this.name = name;<br />

A complete example is given below:<br />

package com.annotation;<br />

import java.lang.annotation.Documented;<br />

import java.lang.annotation.ElementType;<br />

import java.lang.annotation.Inherited;<br />

import java.lang.annotation.Retention;<br />

import java.lang.annotation.RetentionPolicy;<br />

import java.lang.annotation.Target;<br />

@Documented<br />

@Retention(RetentionPolicy.RUNTIME)<br />

@Target({ElementType.TYPE,ElementType.METHOD,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!