salut,
je suis un livre d'A.Goncalves sur Java EE6 et glassfish v3 mais j'ai des problèmes.
j'ai pour but de créer un projet maven contenant un EJB, une entity JPA, et une classe main.
j'ai créé la classe entité (la voici
et la classe EJB (qui accède à la classe JPA via l'injection d'entitymanager) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package lorry.librairie6_ejbproject; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.PersistenceContext; /** * * @author lolveley */ @Entity public class Car implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; public Long getId() { return id; } private String name; /** * Get the value of name * * @return the value of name */ public String getName() { return name; } /** * Set the value of name * * @param name new value of name */ public void setName(String name) { this.name = name; } public void setId(Long id) { this.id = id; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Car)) { return false; } Car other = (Car) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "lorry.librairie6_ejbproject.Car[id=" + id + "]"; } }
jusque-là, rien de très dur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ejb; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import lorry.librairie6_ejbproject.Car; /** * * @author lolveley */ @Stateless public class CarManager implements CarManagerRemote { @PersistenceContext(unitName="lorry.17082010_librairie6_EJBProject_ejb_1.0-SNAPSHOTPU") EntityManager em; public Car save(Car car) { em.persist(car); return car; } public Car find(Long id) { return em.find(Car.class, id); } // Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method") }
voici la classe main , qui est censée accéder à l'EJB par injection :
et quand je lance le projet (je suis sous netbeans) j'ai un message nullpointerexception, au niveau où j'appelle la méthode save.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package start; import ejb.CarManagerRemote; import javax.ejb.EJB; import lorry.librairie6_ejbproject.Car; /** * * @author lolveley */ public class Start { @EJB private static CarManagerRemote carManager; public static void main(String[] args){ Car firstCar=new Car(); firstCar.setId(0L); firstCar.setName("citroën"); carManager.save(firstCar); System.out.println("c fait"); } }
donc pas d'EJB Car en vue.
pouvez-vous m'aider?
voici le pom.xml :
ça serait très sympa de m'aider!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>librairie3_chap06</artifactId> <groupId>lorry.12082010</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>lorry.17082010</groupId> <artifactId>librairie6_EJBProject</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>librairie6_EJBProject Java EE 6 EJB</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>java.net2</id> <name>Java.Net Maven2 Repository, hosts the javaee-api dependency</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <inherited>true</inherited> <configuration> <source>1.6</source> <target>1.6</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifest> <mainClass> start.Start </mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> <properties> <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
olivier
Partager