Skip to content

Commit 6b614f9

Browse files
committed
New sample to show @Index usage
1 parent 9b5e3e7 commit 6b614f9

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed

jpa/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<module>schema-gen-scripts</module>
2929
<module>schema-gen-scripts-external</module>
3030
<module>schema-gen-scripts-generate</module>
31+
<module>schema-gen-index</module>
3132
<module>native-sql</module>
3233
<module>native-sql-resultset-mapping</module>
3334
<module>unsynchronized-pc</module>

jpa/schema-gen-index/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jpa</groupId>
6+
<artifactId>jpa-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>schema-gen-index</artifactId>
12+
<packaging>war</packaging>
13+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.javaee7.jpa.index;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Column;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.Index;
10+
import javax.persistence.NamedQueries;
11+
import javax.persistence.NamedQuery;
12+
import javax.persistence.Table;
13+
14+
/**
15+
* @author Arun Gupta
16+
*/
17+
@Entity
18+
@Table(name = "EMPLOYEE_SCHEMA_GEN_INDEX",
19+
indexes = @Index(columnList="NAME"))
20+
@NamedQueries({
21+
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
22+
})
23+
public class Employee implements Serializable {
24+
private static final long serialVersionUID = 1L;
25+
@Id
26+
@GeneratedValue(strategy = GenerationType.AUTO)
27+
private int id;
28+
29+
@Column(length=40)
30+
private String name;
31+
32+
public Employee() { }
33+
34+
public Employee(String name) {
35+
this.name = name;
36+
}
37+
38+
public int getId() {
39+
return id;
40+
}
41+
42+
public void setId(int id) {
43+
this.id = id;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.jpa.index;
41+
42+
import java.util.List;
43+
import javax.ejb.Stateless;
44+
import javax.persistence.EntityManager;
45+
import javax.persistence.PersistenceContext;
46+
47+
/**
48+
* @author Arun Gupta
49+
*/
50+
@Stateless
51+
public class EmployeeBean {
52+
53+
@PersistenceContext
54+
EntityManager em;
55+
56+
public void persist(Employee e) {
57+
em.persist(e);
58+
}
59+
60+
public List<Employee> get() {
61+
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList();
62+
}
63+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence
3+
version="2.1"
4+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
7+
<persistence-unit name="MyPU" transaction-type="JTA">
8+
<properties>
9+
<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
10+
<property name="javax.persistence.schema-generation.scripts.create-target" value="file:/tmp/create.sql"/>
11+
<property name="javax.persistence.schema-generation.scripts.drop-target" value="file:/tmp/drop.sql"/>
12+
</properties>
13+
</persistence-unit>
14+
</persistence>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!--
2+
/*
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4+
*
5+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
6+
*
7+
* The contents of this file are subject to the terms of either the GNU
8+
* General Public License Version 2 only ("GPL") or the Common Development
9+
* and Distribution License("CDDL") (collectively, the "License"). You
10+
* may not use this file except in compliance with the License. You can
11+
* obtain a copy of the License at
12+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
13+
* or packager/legal/LICENSE.txt. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*
16+
* When distributing the software, include this License Header Notice in each
17+
* file and include the License file at packager/legal/LICENSE.txt.
18+
*
19+
* GPL Classpath Exception:
20+
* Oracle designates this particular file as subject to the "Classpath"
21+
* exception as provided by Oracle in the GPL Version 2 section of the License
22+
* file that accompanied this code.
23+
*
24+
* Modifications:
25+
* If applicable, add the following below the License Header, with the fields
26+
* enclosed by brackets [] replaced by your own identifying information:
27+
* "Portions Copyright [year] [name of copyright owner]"
28+
*
29+
* Contributor(s):
30+
* If you wish your version of this file to be governed by only the CDDL or
31+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
32+
* elects to include this software in this distribution under the [CDDL or GPL
33+
* Version 2] license." If you don't indicate a single choice of license, a
34+
* recipient has the option to distribute your version of this file under
35+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
36+
* its licensees as provided above. However, if you add GPL Version 2 code
37+
* and therefore, elected the GPL Version 2 license, then the option applies
38+
* only if the new code is made subject to such option by the copyright
39+
* holder.
40+
*/
41+
-->
42+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
43+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
44+
"http://www.w3.org/TR/html4/loose.dtd">
45+
46+
<html>
47+
<head>
48+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
49+
<title>JPA 2.1 Schema Generation with @Index</title>
50+
</head>
51+
<body>
52+
<h1>JPA 2.1 Schema Generation with @Index</h1>
53+
54+
<br><br>Check for generated scripts in
55+
<a href="file:///tmp/create.sql">/tmp/create.sql</a> and look for
56+
statements like:
57+
<br><br>
58+
CREATE INDEX INDEX_EMPLOYEE_SCHEMA_GEN_INDEX_NAME ON EMPLOYEE_SCHEMA_GEN_INDEX (NAME)
59+
</body>
60+
</html>

0 commit comments

Comments
 (0)