你好朋友我正在研究Hibernate Framework。我正在尝试在我的应用程序中使用@Embedded Annotation,但我不知道为什么它没有出现在我面前。 这是我的代码
UserDetails.java
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity(name= "USER_DETAILS")
public class UserDetails {
@Id
@Column(name = "USER_NAME")
private String UserName ;
@Column(name = "USER_F_NAME")
private String UserFName ;
@Column(name = "USER_L_NAME")
private String UserLName ;
@Column(name = "USER_CONT_NUMBER")
private String UserContactNumber ;
@Column(name = "USER_AGE")
private int UserAge ;
@Column(name = "JOIN_DATE")
@Temporal(TemporalType.DATE)
private Date joinDate ;
@Embedded
private UserAddress addr ;
public UserAddress getAddr() {
return addr;
}
public void setAddr(UserAddress addr) {
this.addr = addr;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserFName() {
return UserFName;
}
public void setUserFName(String userFName) {
UserFName = userFName;
}
public String getUserLName() {
return UserLName;
}
public void setUserLName(String userLName) {
UserLName = userLName;
}
public String getUserContactNumber() {
return UserContactNumber;
}
public void setUserContactNumber(String userContactNumber) {
UserContactNumber = userContactNumber;
}
public int getUserAge() {
return UserAge;
}
public void setUserAge(int userAge) {
UserAge = userAge;
}
}
UserAddress.java
package org.hibernate.model;
import javax.persistence.Embeddable;
@Embeddable
public class UserAddress {
private String Street ;
private String City ;
private String State ;
private String Country ;
private String Pincode ;
public String getStreet() {
return Street;
}
public void setStreet(String street) {
Street = street;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
public String getPincode() {
return Pincode;
}
public void setPincode(String pincode) {
Pincode = pincode;
}
}
UserController.java
package org.hibernate.controller;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.model.UserAddress;
import org.hibernate.model.UserDetails;
public class UserController {
public static void main(String[] args) {
UserDetails ud = new UserDetails();
ud.setUserFName("Pritesh");
ud.setUserLName("Patel");
ud.setUserName("ppatel123");
ud.setUserAge(26);
ud.setUserContactNumber("6479644065");
ud.setJoinDate(new Date());
UserAddress addr = new UserAddress();
addr.setStreet("Stella Drive");
addr.setCity("Markham");
addr.setState("Ontario");
addr.setCountry("Canada");
addr.setPincode("L6X 5L7");
ud.setAddr(addr);
SessionFactory sf = new Configuration().configure().buildSessionFactory();
System.out.println("Loading done");
Session session = sf.openSession();
System.out.println("ransaction begin");
session.beginTransaction();
session.save(ud);
session.getTransaction().commit();
session.close();
ud = null ;
session = sf.openSession();
session.beginTransaction();
ud = (UserDetails)session.get(UserDetails.class, "ppatel123");
System.out.println("User Id " + ud.getUserName());
System.out.println("User Name " + ud.getUserFName()+ " " + ud.getUserLName() );
System.out.println("User Age " + ud.getUserAge());
System.out.println("User Contact Number " + ud.getUserContactNumber());
System.out.println("User Joining Date " + ud.getJoinDate());
}
}
的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/online_store</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="hbm2ddl.auto">create</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping class="org.hibernate.model.UserDetails" />
</session-factory>
</hibernate-configuration>
输出控制台
Oct 19, 2016 2:13:41 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.3.Final}
Oct 19, 2016 2:13:41 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 19, 2016 2:13:41 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 19, 2016 2:13:43 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Oct 19, 2016 2:13:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Oct 19, 2016 2:13:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/online_store]
Oct 19, 2016 2:13:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Oct 19, 2016 2:13:43 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Oct 19, 2016 2:13:43 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Wed Oct 19 14:13:43 EDT 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Oct 19, 2016 2:13:44 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Hibernate: drop table if exists USER_DETAILS
Oct 19, 2016 2:13:46 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@517566b] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
在它工作正常之前,但是当我开始使用@Embedded Annotation时,我的输出控制台只显示了这一点。 在此先感谢
答案 0 :(得分:0)
您在UserDetail和UserAddress类上复制了相同的代码......所以,我不确定,但您可能忘记在用户地址类上添加@Embeddable标记。如果您可以在UserAddress上放置正确的代码,可能会看到其他任何内容