高分求教主键如何设置可以支持两种数据库? 比如同时支持oracle和sql server?请教大侠高手指点!(不用字符串) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用程序创建的话,建议用JPA,主键属性上声明个Annotation标签,系统自动帮你创建 你都两个数据库了 那就可以生成两个主键 关键的时候 你要在程序中写代码啊 如果是sql数据库的话 让代码执行 你要生成的主键 如果是oracle的话 让代码另外执行 不就多一个if else 这是我收藏的一个主键生成类,楼主可以拿去用:import java.io.Serializable;import java.net.InetAddress;/** * 生成类似hibernate中uuid 32位主键序列 * * @version: V1.0 */public class UUIDGenerator { private static final int IP; public static int IptoInt(byte[] bytes) { int result = 0; for (int i = 0; i < 4; i++) { result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i]; } return result; } static { int ipadd; try { ipadd = IptoInt(InetAddress.getLocalHost().getAddress()); } catch (Exception e) { ipadd = 0; } IP = ipadd; } private static short counter = (short) 0; private static final int JVM = (int) (System.currentTimeMillis() >>> 8); public UUIDGenerator() { } public static int getJVM() { return JVM; } public static short getCount() { synchronized (UUIDGenerator.class) { if (counter < 0) counter = 0; return counter++; } } public static int getIP() { return IP; } public static short getHiTime() { return (short) (System.currentTimeMillis() >>> 32); } public static int getLoTime() { return (int) System.currentTimeMillis(); } private final static String sep = ""; public static String format(int intval) { String formatted = Integer.toHexString(intval); StringBuffer buf = new StringBuffer("00000000"); buf.replace(8 - formatted.length(), 8, formatted); return buf.toString(); } public static String format(short shortval) { String formatted = Integer.toHexString(shortval); StringBuffer buf = new StringBuffer("0000"); buf.replace(4 - formatted.length(), 4, formatted); return buf.toString(); } public static String generate() { return String.valueOf(new StringBuffer(36).append(format(getIP())).append(sep) .append(format(getJVM())).append(sep) .append(format(getHiTime())).append(sep) .append(format(getLoTime())).append(sep) .append(format(getCount())).toString()); } public static void main(String args[]){ System.out.println(UUIDGenerator.generate()); }} @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;配置sessionFactory时,有packagesToScan属性: <property name="packagesToScan"> <list> <value>com.jandar.domain</value> </list> </property>另外,首次加载应用时,hibernate.hbm2ddl.auto设置成create,以后设置成update或none。 可以自己写一个主键生成器,获取当前的时间时分秒:yyyymmddHHMMSS,后面还可以根据需要加个几位的随机字符串 关于LinkedList的问题 jdbc问题 hibernate多表查询如何把值传到jsp 导出hibernate中的导出映射文件 快就业了,大家点建议把。 struts+spring+hibernate 不能写入数据库,在线等急!!! 关于jsp从数据库中存储和读出图片的问题 菜鸟发问 如何配置JavaMail环境 在jb8中写一下程序,为何非要我抛出异常 怎么用el表达式获取方法 tomcat应用读取服务器上的中文文件名为乱码 请各位大哥大嫂指教
import java.io.Serializable;
import java.net.InetAddress;/**
* 生成类似hibernate中uuid 32位主键序列
*
* @version: V1.0
*/
public class UUIDGenerator { private static final int IP; public static int IptoInt(byte[] bytes) {
int result = 0;
for (int i = 0; i < 4; i++) {
result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
}
return result;
} static {
int ipadd;
try {
ipadd = IptoInt(InetAddress.getLocalHost().getAddress());
} catch (Exception e) {
ipadd = 0;
}
IP = ipadd;
}
private static short counter = (short) 0;
private static final int JVM = (int) (System.currentTimeMillis() >>> 8); public UUIDGenerator() {
}
public static int getJVM() {
return JVM;
}
public static short getCount() {
synchronized (UUIDGenerator.class) {
if (counter < 0)
counter = 0;
return counter++;
}
}
public static int getIP() {
return IP;
}
public static short getHiTime() {
return (short) (System.currentTimeMillis() >>> 32);
} public static int getLoTime() {
return (int) System.currentTimeMillis();
} private final static String sep = ""; public static String format(int intval) {
String formatted = Integer.toHexString(intval);
StringBuffer buf = new StringBuffer("00000000");
buf.replace(8 - formatted.length(), 8, formatted);
return buf.toString();
} public static String format(short shortval) {
String formatted = Integer.toHexString(shortval);
StringBuffer buf = new StringBuffer("0000");
buf.replace(4 - formatted.length(), 4, formatted);
return buf.toString();
} public static String generate() {
return String.valueOf(new StringBuffer(36).append(format(getIP())).append(sep)
.append(format(getJVM())).append(sep)
.append(format(getHiTime())).append(sep)
.append(format(getLoTime())).append(sep)
.append(format(getCount())).toString());
}
public static void main(String args[]){
System.out.println(UUIDGenerator.generate());
}
}
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
配置sessionFactory时,有packagesToScan属性: <property name="packagesToScan">
<list>
<value>com.jandar.domain</value>
</list>
</property>
另外,首次加载应用时,hibernate.hbm2ddl.auto设置成create,以后设置成update或none。